Python 从 JSON 转换为 CSV 时出现 3 个编码问题
Python 3 Encoding Issues when converting to CSV from JSON
Python3
我看过其他解决方案,但他们似乎没有涵盖我的情况。我负责编写脚本以获取 JSON 并将其转换为 CSV 文件。
我已经完成了大部分工作,但在写入数据时遇到了问题。我收到的数据与写入的不符。下面是一个例子。我不知道如何才能保留编码。
我应该提到默认编码是 UTF-8
输入:necesitará
输出:necesitar×
import csv
import json
import sys
import sys
print(sys.getdefaultencoding())
stuff = open('data.json')
jsonStuff = json.loads(stuff.read(), encoding="utf-8")
with open('output.csv', 'w', newline='\n', encoding='utf-8') as csvfile:
writer = csv.writer(csvfile, delimiter=",",quotechar='"',quoting=csv.QUOTE_MINIMAL)
for element in jsonStuff:
row = ""
key = element['key']
values = element['valuesRow']
row = element['key']
# values[0]['value'], values[1]['value'], values[2]['value'], values[3]['value'],
writer.writerow([element['key'], values[3]['value']])
删除 open('output.csv', 'w', newline='\n', encoding='utf-8')
中的 encoding='utf-8'
应该可以修复它。
data.json (utf-8): {"first": "necesitará", "second": "bodø"}
以下...
import csv
import json
with open('data.json') as stuff, open('output.csv', 'w', newline='\n', encoding='utf-8') as csvfile:
jsonStuff = json.loads(stuff.read(), encoding="utf-8")
writer = csv.writer(csvfile, delimiter=",", quotechar='"', quoting=csv.QUOTE_MINIMAL)
first = jsonStuff['first']
second = jsonStuff['second']
writer.writerow([first, second])
... 给出 output.csv: necesitará,bodø
然而...
import csv
import json
with open('data.json') as stuff, open('output.csv', 'w', newline='\n') as csvfile:
jsonStuff = json.loads(stuff.read(), encoding="utf-8")
writer = csv.writer(csvfile, delimiter=",", quotechar='"', quoting=csv.QUOTE_MINIMAL)
first = jsonStuff['first']
second = jsonStuff['second']
writer.writerow([first, second])
... 产生 output.csv:necesitará,bodø
话虽如此。当您有 json.load()
时,没有理由使用 json.loads()
,并且您定义的大部分内容都是默认值。我只是做...
import csv
import json
with open('data.json') as jsonfile, open('output.csv', 'w') as csvfile:
json_data = json.load(jsonfile)
writer = csv.writer(csvfile, quoting=csv.QUOTE_MINIMAL)
first = json_data['first']
second = json_data['second']
writer.writerow([first, second])
Python3
我看过其他解决方案,但他们似乎没有涵盖我的情况。我负责编写脚本以获取 JSON 并将其转换为 CSV 文件。
我已经完成了大部分工作,但在写入数据时遇到了问题。我收到的数据与写入的不符。下面是一个例子。我不知道如何才能保留编码。
我应该提到默认编码是 UTF-8
输入:necesitará
输出:necesitar×
import csv
import json
import sys
import sys
print(sys.getdefaultencoding())
stuff = open('data.json')
jsonStuff = json.loads(stuff.read(), encoding="utf-8")
with open('output.csv', 'w', newline='\n', encoding='utf-8') as csvfile:
writer = csv.writer(csvfile, delimiter=",",quotechar='"',quoting=csv.QUOTE_MINIMAL)
for element in jsonStuff:
row = ""
key = element['key']
values = element['valuesRow']
row = element['key']
# values[0]['value'], values[1]['value'], values[2]['value'], values[3]['value'],
writer.writerow([element['key'], values[3]['value']])
删除 open('output.csv', 'w', newline='\n', encoding='utf-8')
中的 encoding='utf-8'
应该可以修复它。
data.json (utf-8): {"first": "necesitará", "second": "bodø"}
以下...
import csv
import json
with open('data.json') as stuff, open('output.csv', 'w', newline='\n', encoding='utf-8') as csvfile:
jsonStuff = json.loads(stuff.read(), encoding="utf-8")
writer = csv.writer(csvfile, delimiter=",", quotechar='"', quoting=csv.QUOTE_MINIMAL)
first = jsonStuff['first']
second = jsonStuff['second']
writer.writerow([first, second])
... 给出 output.csv: necesitará,bodø
然而...
import csv
import json
with open('data.json') as stuff, open('output.csv', 'w', newline='\n') as csvfile:
jsonStuff = json.loads(stuff.read(), encoding="utf-8")
writer = csv.writer(csvfile, delimiter=",", quotechar='"', quoting=csv.QUOTE_MINIMAL)
first = jsonStuff['first']
second = jsonStuff['second']
writer.writerow([first, second])
... 产生 output.csv:necesitará,bodø
话虽如此。当您有 json.load()
时,没有理由使用 json.loads()
,并且您定义的大部分内容都是默认值。我只是做...
import csv
import json
with open('data.json') as jsonfile, open('output.csv', 'w') as csvfile:
json_data = json.load(jsonfile)
writer = csv.writer(csvfile, quoting=csv.QUOTE_MINIMAL)
first = json_data['first']
second = json_data['second']
writer.writerow([first, second])