PythonでCSVファイルをJSONファイルに書き換える時に文字エンコーディングのエラーが生じてしまいます。
使用しているコードは以下になります。
#coding:utf-8 import csv, json filename = 'test_code' 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 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()
使用しているCSVファイルは以下になります(実際は200行ほど続きますが、ここには一部のみ表示します)。
文字エンコーディングはutf-8です。
cityid name id141011 札幌市 id141020 釧路市 id141038 函館市
このままpythonプログラムを実行すると、以下のエラーが表示されます。
Traceback (most recent call last): File "conv_csv_to_json.py", line 37, in <module> json.dump(data, f, ensure_ascii=False, indent=2, encoding='utf8') File "c:\Python27\lib\json\__init__.py", line 190, in dump fp.write(chunk) UnicodeEncodeError: 'ascii' codec can't encode characters in position 1-3: ordinal not in range(128)
このエラーから、pythonコードの以下の所に間違いがあると考えられます。
json.dump(data, f, ensure_ascii=False, indent=2, encoding='utf8')
なので、
json.dump(data, f, ensure_ascii=False, indent=2)
や
json.dump(data, f, ensure_ascii=False, indent=2).encode('utf-8')
とコードを変えて試しているのですが、改善されません。
どのようにすればこのエラーを解決できるでしょうか。
わかる方がおりましたら、よろしくお願い致します。
追記
そのままのコードを実行した場合、以下のjsonファイルが作成されます
[ { "cityid": "id141011", "name":
漢字のところでエラーが生じ、それ以降は処理されていません。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2015/12/04 03:56
2015/12/04 04:04
2015/12/04 04:09