[Python] How to store a csv file as one-dimensional array data

Purpose

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.

Method

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)

result

['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

[Python] How to store a csv file as one-dimensional array data
[Python] How to convert db file to csv
How to disguise a ZIP file as a PNG file
How to create a JSON file in Python
I'm addicted to Kintone as a data store
[Python] How to read a csv file (read_csv method of pandas module)
[Python] How to scrape a local html file and output it as CSV using Beautiful Soup
How to save a table scraped by python to csv
Python script to create a JSON file from a CSV file
How to slice a block multiple array from a multiple array in Python
How to convert an array to a dictionary with Python [Application]
How to run a Python file at a Windows 10 command prompt
How to import a file anywhere you like in Python
How to transpose a 2D array using only python [Note]
How to use a file other than .fabricrc as a configuration file
[Python] How to output a pandas table to an excel file
A python script that converts Oracle Database data to csv
How to write a Python class
Output to csv file with Python
[Python] How to swap array values
How to create a config file
How to paste a CSV file into an Excel file using Pandas
How to make a string into an array or an array into a string in Python
How to update a Tableau packaged workbook data source using Python
How to create a CSV dummy file containing Japanese using Faker
[Python] Read a csv file with a large data size using a generator
[ROS2] How to play a bag file with python format launch
[Python] How to make a class iterable
[Python] How to convert a 2D list to a 1D list
How to convert Python to an exe file
[Python] Convert csv file delimiters to tab delimiters
[Python] How to invert a character string
How to get a stacktrace in python
How to use "deque" for Python data
Download Pandas DataFrame as a CSV file
How to run a Maya Python script
How to send a visualization image of data created in Python to Typetalk
How to get a value from a parameter store in lambda (using python)
How to install python package in local environment as a general user
How to put a line number at the beginning of a CSV file
Read CSV file with Python and convert it to DataFrame as it is
How to sort by specifying a column in the Python Numpy array.
How to create sample CSV data with hypothesis
Python C / C ++ Extension Pattern-Pass data to Python as np.array
How to open a web browser from python
How to clear tuples in a list (Python)
Just add the python array to the json data
Writing logs to CSV file (Python, C language)
[Python] How to read data from CIFAR-10 and CIFAR-100
How to generate a Python object from JSON
[Introduction to Python] How to handle JSON format data
How to add a Python module search path
How to notify a Discord channel in Python
Export Python3 version OpenCV KeyPoint to a file
Write CSV data to AWS-S3 with AWS-Lambda + Python
[Python] How to draw a histogram in Matplotlib
How to output "Ketsumaimo" as standard output in Python
I want to write to a file with Python
How to read a file in a different directory
Various ways to read the last line of a csv file in Python
Summary of how to read numerical data with python [CSV, NetCDF, Fortran binary]