Un moyen simple de lire des fichiers CSV à partir de programmes Python.
Facile à utiliser avec le module standard de la bibliothèque csv
.
Sample Fonctionne avec Python 2/3. Il existe un support japonais.
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,Agence de la consommation,Administratif et financier,Annuel,PDF,Anglais,1
12971,2015,Agence de la consommation,Administratif et financier,Annuel,HTML,Japonais,59
12971,2015,Agence de la consommation,Administratif et financier,Annuel,PDF,Japonais,4
12972,2015,Agence de la consommation,Administratif et financier,Annuel,HTML,Japonais,3
12972,2015,Agence de la consommation,Administratif et financier,Annuel,PDF,Japonais,6
12973,2015,Agence de la consommation,Administratif et financier,Annuel,PDF,Japonais,6
12974,2015,Agence de la consommation,Administratif et financier,Annuel,PDF,Japonais,4
12975,2015,Agence de la consommation,Administratif et financier,Autre (description gratuite),PDF,Japonais,7
12976,2015,Agence de la consommation,Administratif et financier,Autre (description gratuite),PDF,Japonais,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', 'Agence de la consommation', 'Administratif et financier', 'Annuel', 'PDF', 'Anglais', '1']
['12971', '2015', 'Agence de la consommation', 'Administratif et financier', 'Annuel', 'HTML', 'Japonais', '59']
['12971', '2015', 'Agence de la consommation', 'Administratif et financier', 'Annuel', 'PDF', 'Japonais', '4']
['12972', '2015', 'Agence de la consommation', 'Administratif et financier', 'Annuel', 'HTML', 'Japonais', '3']
['12972', '2015', 'Agence de la consommation', 'Administratif et financier', 'Annuel', 'PDF', 'Japonais', '6']
['12973', '2015', 'Agence de la consommation', 'Administratif et financier', 'Annuel', 'PDF', 'Japonais', '6']
['12974', '2015', 'Agence de la consommation', 'Administratif et financier', 'Annuel', 'PDF', 'Japonais', '4']
['12975', '2015', 'Agence de la consommation', 'Administratif et financier', 'Autre (description gratuite)', 'PDF', 'Japonais', '7']
['12976', '2015', 'Agence de la consommation', 'Administratif et financier', 'Autre (description gratuite)', 'PDF', 'Japonais', '4']
encoding - Python 2 and 3 csv reader - Stack Overflow http://stackoverflow.com/questions/5180555/python-2-and-3-csv-reader
Recommended Posts