An easy way to read a CSV file from a Python program.
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)
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']
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']
encoding - Python 2 and 3 csv reader - Stack Overflow http://stackoverflow.com/questions/5180555/python-2-and-3-csv-reader
Recommended Posts