I have a file example.csv
with the contents
1,"A towel,",1.0 42," it says, ",2.0 1337,is about the most ,-1 0,massively useful thing ,123 -2,an interstellar hitchhiker can have.,3
How do I read this example.csv
with Python?
Similarly, if I have
data = [(1, "A towel,", 1.0), (42, " it says, ", 2.0), (1337, "is about the most ", -1), (0, "massively useful thing ", 123), (-2, "an interstellar hitchhiker can have.", 3)]
How do I write data
to a CSV file with Python?
Read A CSV File Using Python There are two common ways to read a . csv file when using Python. The first by using the csv library, and the second by using the pandas library.
Here are some minimal complete examples how to read CSV files and how to write CSV files with Python.
Pure Python
import csv # Define data data = [ (1, "A towel,", 1.0), (42, " it says, ", 2.0), (1337, "is about the most ", -1), (0, "massively useful thing ", 123), (-2, "an interstellar hitchhiker can have.", 3), ] # Write CSV file with open("test.csv", "wt") as fp: writer = csv.writer(fp, delimiter=",") # writer.writerow(["your", "header", "foo"]) # write header writer.writerows(data) # Read CSV file with open("test.csv") as fp: reader = csv.reader(fp, delimiter=",", quotechar='"') # next(reader, None) # skip the headers data_read = [row for row in reader] print(data_read)
After that, the contents of data_read
are
[['1', 'A towel,', '1.0'], ['42', ' it says, ', '2.0'], ['1337', 'is about the most ', '-1'], ['0', 'massively useful thing ', '123'], ['-2', 'an interstellar hitchhiker can have.', '3']]
Please note that CSV reads only strings. You need to convert to the column types manually.
A Python 2+3 version was here before (link), but Python 2 support is dropped. Removing the Python 2 stuff massively simplified this answer.
Have a look at my utility package mpu
for a super simple and easy to remember one:
import mpu.io data = mpu.io.read('example.csv', delimiter=',', quotechar='"', skiprows=None) mpu.io.write('example.csv', data)
import pandas as pd # Read the CSV into a pandas data frame (df) # With a df you can do many things # most important: visualize data with Seaborn df = pd.read_csv('myfile.csv', sep=',') print(df) # Or export it in many ways, e.g. a list of tuples tuples = [tuple(x) for x in df.values] # or export it as a list of dicts dicts = df.to_dict().values()
See read_csv
docs for more information. Please note that pandas automatically infers if there is a header line, but you can set it manually, too.
If you haven't heard of Seaborn, I recommend having a look at it.
Reading CSV files is supported by a bunch of other libraries, for example:
dask.dataframe.read_csv
spark.read.csv
1,"A towel,",1.0 42," it says, ",2.0 1337,is about the most ,-1 0,massively useful thing ,123 -2,an interstellar hitchhiker can have.,3
.csv
After reading the CSV file to a list of tuples / dicts or a Pandas dataframe, it is simply working with this kind of data. Nothing CSV specific.
For your application, the following might be important:
See also: Comparison of data serialization formats
In case you are rather looking for a way to make configuration files, you might want to read my short article Configuration files in Python
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With