sample.csv
a,b,c,d,e
Store the above csv file in the following one-dimensional array.
['a','b','c','d','e']
I created a csv saved as a result of label data classified by machine learning to handle it in a python program, but if there is a better way, please let me know in the comments.
If sample.csv and read_csv_flatten.py are in the same directory, the contents of csv will be stored in data in a one-dimensional array by executing the following code. If it is not in the same directory, you need to specify an appropriate path.
read_csv_flatten.py
import csv
import numpy as np
with open("sample.csv") as fp:
reader = csv.reader(fp)
data = [ e for e in reader ]
data = np.array(data).reshape(-1)
#Output for confirmation below
print(data)
['a' 'b' 'c' 'd' 'e']
As mentioned above, it can be seen that the contents of csv are stored in a one-dimensional array.
Recommended Posts