Python / JSON-加载带有重音符号的json文件时出错。

时间:2018-10-11 17:05:05

标签: python json file character-encoding

我目前有一个要在python中加载的JSON文件,但是由于带有重音符号,我遇到了错误。

在加载之前,有没有办法将重音符号替换为常规字符?

我根据其他帖子建议尝试了所有编码为UTF-8的方法。 例如:

json.loads(line.decode("utf-8","ignore"))

with open("test.json", encoding='utf-8') as data_file:                           
    data = json.load(data_file)

由于所有这些选项均无效,因此我想查看是否存在一种替换重音符的方法。还是我解码不正确?

示例代码:

import csv
import json

infile = open("infile.json","r") 
outfile = open("outfile.csv", "w") 

writer = csv.writer(outfile)
readin = json.loads(infile.read())
count = 1

for row in readin["entry1"]["entry2"]:

    if count == 1:
        writer.writerow(row.keys())
        count = count + 1

    writer.writerow(row.values())

outfile.close()

以上方法有效,仅当绝对没有重音字符时才有效。

感谢任何帮助。

谢谢。

0 个答案:

没有答案
相关问题