How to import a csv-file into a data array?

前端 未结 2 660
悲哀的现实
悲哀的现实 2021-01-01 18:40

I have a line of code in a script that imports data from a text file with lots of spaces between values into an array for use later.

textfile = open(\'file.t         


        
2条回答
  •  时光取名叫无心
    2021-01-01 19:02

    You can use pandas library or numpy to read the CSV file. If your file is tab-separated then use '\t' in place of comma in both sep and delimiter arguments below.

    import pandas as pd 
    myFile = pd.read_csv('filepath', sep=',')
    

    Or

     import numpy as np
     myFile = np.genfromtxt('filepath', delimiter=',')
    

提交回复
热议问题