Im trying to get the data stored in a file and put that data into a parallel array.
How to read a file into a parallel array?
Collapse
X
-
Since the file type is not written, I will show you how to read the csv file.
1.Use Pandas
2.Use csvCode:import pandas as pd filename = 'sample.csv' # with headder #df = pd.read_csv(filename, header=0) # without headder df = pd.read_csv(filename, header=None, names=['2nd', '3rd']) valuelist1 = df.index.values valuelist2 = df.loc[:,'2nd'].values valuelist3 = df.loc[:,'3rd'].values print(valuelist1) print(valuelist2) print(valuelist3)
Code:import csv filename = 'sample.csv' valuelist1 = [] valuelist2 = [] valuelist3 = [] with open(filename) as f: reader = csv.reader(f) for row in reader: valuelist1.append(row[0]) valuelist2.append(row[1]) valuelist3.append(row[2]) print(valuelist1) print(valuelist2) print(valuelist3)
-
pandas
sep = ‘,’ specifies the delimiter with a comma.Code:df = pd.read_csv(filename, header=None, sep=',', names=['2nd', '3rd'])
csv
delimiter = ‘,’ specifies the delimiter with a comma.Code:csv.reader (filename, delimiter =',')
Comment
Comment