Created
June 14, 2014 12:05
-
-
Save hitsujixgit/82743ce5eecbf1b02c69 to your computer and use it in GitHub Desktop.
Read CSV file and convert it to JSON format (UTF-8 encoding)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#coding:utf-8 | |
''' | |
Created on 2014/03/27 | |
@author: Tae Matsumoto | |
CSVファイルを読み込み、同名のJSONファイルを出力します。 | |
''' | |
import csv, json | |
filename = 'yokohama_codes' | |
header = [] | |
data = [] | |
def unicode_csv_reader(unicode_csv_data, dialect=csv.excel, **kwargs): | |
# csv.py doesn't do Unicode; encode temporarily as UTF-8: | |
csv_reader = csv.reader(utf_8_encoder(unicode_csv_data),dialect=dialect, **kwargs) | |
for row in csv_reader: | |
# decode UTF-8 back to Unicode, cell by cell: | |
yield [unicode(cell, 'utf-8') for cell in row] | |
def utf_8_encoder(unicode_csv_data): | |
for line in unicode_csv_data: | |
yield line.encode('utf-8') | |
with open(filename + '.csv', 'rU') as csvfile: | |
spamreader = unicode_csv_reader(csvfile, dialect=csv.excel) | |
is_first = True | |
for row in spamreader: | |
if is_first: | |
header = row[:] | |
is_first = False | |
continue | |
items = {} | |
for i in range(0, len(row)): | |
item = row[i] | |
items[header[i]] = item | |
data.append(items) | |
with open(filename+'.json', 'w') as f: | |
json.dump(data, f, ensure_ascii=False, indent=2, encoding='utf8') | |
f.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment