How to read a CSV file with Python 2/3

An easy way to read a CSV file from a Python program.

Method

Easy to use with the standard library csv module.

Sample Works with both Python 2/3. Japanese is available.

https://github.com/thombashi/PythonExamples/tree/master/csv

csv_read.py


from __future__ import print_function
from __future__ import unicode_literals
import argparse
import csv
import io

import six


parser = argparse.ArgumentParser()
parser.add_argument("csv_file_path")
parser.add_argument("--encoding", default="utf_8")
options = parser.parse_args()

csv_reader = csv.reader(
    io.open(options.csv_file_path, "r", encoding=options.encoding),
    delimiter=",",
    quotechar='"'
)

print("--- header ---\n{}\n".format(six.next(csv_reader)))
print("--- data ---")
for row in csv_reader:
    print(row)

Example of operation: ascii

sample_ascii.csv


"Country or Area","Year","Area","Sex","Record Type","Reliability","Source Year","Value","Value Footnotes"
"Afghanistan","2014","Total","Both Sexes","Estimate - de facto","Final figure, incomplete/questionable reliability","2015","26556754","1"
"Afghanistan","2014","Total","Male","Estimate - de facto","Final figure, incomplete/questionable reliability","2015","13585933","1"
"Afghanistan","2014","Total","Female","Estimate - de facto","Final figure, incomplete/questionable reliability","2015","12970821","1"
$ ./csv_read.py sample_ascii.csv
--- header ---
['Country or Area', 'Year', 'Area', 'Sex', 'Record Type', 'Reliability', 'Source Year', 'Value', 'Value Footnotes']

--- data ---
['Afghanistan', '2014', 'Total', 'Both Sexes', 'Estimate - de facto', 'Final figure, incomplete/questionable reliability', '2015', '26556754', '1']
['Afghanistan', '2014', 'Total', 'Male', 'Estimate - de facto', 'Final figure, incomplete/questionable reliability', '2015', '13585933', '1']
['Afghanistan', '2014', 'Total', 'Female', 'Estimate - de facto', 'Final figure, incomplete/questionable reliability', '2015', '12970821', '1']

Example of operation: UTF-8

sample_utf8.csv


dataset_id,year,publisher,group_title,frequency_of_update,data_format,language,resource_count
12971,2015,Consumer Affairs Agency,Administrative and financial,Yearly,PDF,English,1
12971,2015,Consumer Affairs Agency,Administrative and financial,Yearly,HTML,Japanese,59
12971,2015,Consumer Affairs Agency,Administrative and financial,Yearly,PDF,Japanese,4
12972,2015,Consumer Affairs Agency,Administrative and financial,Yearly,HTML,Japanese,3
12972,2015,Consumer Affairs Agency,Administrative and financial,Yearly,PDF,Japanese,6
12973,2015,Consumer Affairs Agency,Administrative and financial,Yearly,PDF,Japanese,6
12974,2015,Consumer Affairs Agency,Administrative and financial,Yearly,PDF,Japanese,4
12975,2015,Consumer Affairs Agency,Administrative and financial,Other (free description),PDF,Japanese,7
12976,2015,Consumer Affairs Agency,Administrative and financial,Other (free description),PDF,Japanese,4
$ ./csv_read.py sample_utf8.csv
--- header ---
['dataset_id', 'year', 'publisher', 'group_title', 'frequency_of_update', 'data_format', 'language', 'resource_count']

--- data ---
['12971', '2015', 'Consumer Affairs Agency', 'Administrative and financial', 'Yearly', 'PDF', 'English', '1']
['12971', '2015', 'Consumer Affairs Agency', 'Administrative and financial', 'Yearly', 'HTML', 'Japanese', '59']
['12971', '2015', 'Consumer Affairs Agency', 'Administrative and financial', 'Yearly', 'PDF', 'Japanese', '4']
['12972', '2015', 'Consumer Affairs Agency', 'Administrative and financial', 'Yearly', 'HTML', 'Japanese', '3']
['12972', '2015', 'Consumer Affairs Agency', 'Administrative and financial', 'Yearly', 'PDF', 'Japanese', '6']
['12973', '2015', 'Consumer Affairs Agency', 'Administrative and financial', 'Yearly', 'PDF', 'Japanese', '6']
['12974', '2015', 'Consumer Affairs Agency', 'Administrative and financial', 'Yearly', 'PDF', 'Japanese', '4']
['12975', '2015', 'Consumer Affairs Agency', 'Administrative and financial', 'Other (free description)', 'PDF', 'Japanese', '7']
['12976', '2015', 'Consumer Affairs Agency', 'Administrative and financial', 'Other (free description)', 'PDF', 'Japanese', '4']

reference

encoding - Python 2 and 3 csv reader - Stack Overflow http://stackoverflow.com/questions/5180555/python-2-and-3-csv-reader

Recommended Posts

How to read a CSV file with Python 2/3
[Python] How to read excel file with pandas
[Python] Write to csv file with Python
Output to csv file with Python
How to convert JSON file to CSV file with Python Pandas
[Python] How to read a csv file (read_csv method of pandas module)
[Python] How to convert db file to csv
Read CSV file with python (Download & parse CSV file)
How to read an Excel file (.xlsx) with Pandas [Python]
Read Python csv file
How to make a command to read the configuration file with pyramid
[Python] How to store a csv file as one-dimensional array data
[Python] Read a csv file with a large data size using a generator
[ROS2] How to play a bag file with python format launch
How to create a JSON file in Python
Read line by line from a file with Python
I want to write to a file with Python
How to read a file in a different directory
Read csv with python pandas
Write to csv with Python
Download csv file with python
Read and format a csv file mixed with comma tabs with Python pandas
Read CSV file with Python and convert it to DataFrame as it is
How to convert / restore a string with [] in python
[Python] How to draw a line graph with Matplotlib
I tried to touch the CSV file with Python
How to measure mp3 file playback time with python
[Python] How to deal with pandas read_html read error
[Python] A memo to write CSV vertically with Pandas
How to save a table scraped by python to csv
Python script to create a JSON file from a CSV file
[Python] How to create a 2D histogram with Matplotlib
How to read csv containing only integers in Python
[Python] How to draw a scatter plot with Matplotlib
Various ways to read the last line of a csv file in Python
How to write a Python class
Python: How to use async with
Summary of how to read numerical data with python [CSV, NetCDF, Fortran binary]
How to drop Google Docs in one folder in a .txt file with python
High resolution acoustic signal processing (1) --How to read 24-bit wav file with Python
How to get started with Python
How to use FTP with Python
How to calculate date with python
How to create a config file
How to put a hyperlink to "file: // hogehoge" with sphinx-> pdf
How to install NPI + send a message to line with python
How to switch the configuration file to be read by Python
How to convert an array to a dictionary with Python [Application]
How to run a Python file at a Windows 10 command prompt
[Python] Read the csv file and display the figure with matplotlib
How to import a file anywhere you like in Python
How to build a python2.7 series development environment with Vagrant
[Python Kivy] How to create an exe file with pyinstaller
[Python] How to output a pandas table to an excel file
Creating a simple PowerPoint file with Python
How to read pydoc on python interpreter
How to add a package with PyCharm
[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