如何从csv文件中读取梵文字符并将其原样写在josn上?

时间:2019-01-17 04:16:22

标签: python json python-3.x csv

我有以下python脚本来读取csv文件并将其数据保存到json文件中。

import csv
import json
import sys

with open(sys.argv[1], 'r') as csv_file:
    csv_content = csv.DictReader(csv_file)   
    with open(sys.argv[2], 'w') as new_file:
        new_file.write('[\n')
        for row in csv_content:
            json.dump(row, new_file)
            new_file.write(',\n')
        new_file.write(']')

哪些语言适用于英文字符,但对梵文字符会抛出异常。

然后我将代码更改为

import csv
import json
import sys

with open(sys.argv[1], encoding='utf-8', mode='r') as csv_file:
    csv_content = csv.DictReader(csv_file)   
    with open(sys.argv[2], encoding='utf-8', mode='w') as new_file:
        new_file.write('[\n')
        for row in csv_content:
            json.dump(row, new_file)
            new_file.write(',\n')
        new_file.write(']') 

哪个生成的

id,name,alpha2,alpha3
524,नेपाल,np,npl

{"\ufeffid": "524", "name": "\u0928\u0947\u092a\u093e\u0932", "alpha2": "np", "alpha3": "npl"},

基本上我想要的是

{"id": "524", "name": "नेपाल", "alpha2": "np", "alpha3": "npl"},

因此。

0 个答案:

没有答案