[英](Python) Convert CSV file to JSON
我尝试将 csv 文件转换为 Json。dataframe 如下所示。
但是当我加载一个 Json 文件时,我看到 '성' 这个 unicode 类型。
我的问题是,需要做什么才能看到韩语原文?
data_id 성별 나이(개월) 촬영 장소
0 29704982 암컷 360 실내
1 30009189 암컷 48 실내
2 29778525 수컷 18 실내
3 29793292 암컷 120 실내
4 30091656 수컷 20 실내
5 30091656 수컷 144 실외
6 29704982 암컷 198 실외
7 29793292 수컷 43 실외
8 30009189 수컷 72 실내
9 30091656 수컷 6 실내
#my use case
json_file = dict(csv_file.set_index('data_id').groupby(level=0).apply(lambda x : x.to_json(orient = 'records')))
print("Unicode Data is ", json_file)
print("Unicode JSON Data encoding using utf-8")
encodedUnicode = json.dumps(json_file, ensure_ascii=False).encode('utf-8')
print("Decoding JSON", json.loads(encodedUnicode))
运行这段代码后,我仍然得到 unicode 格式的结果。
unicode Data is {29704982: '[{"\\uc131\\ubcc4":"\\uc554\\ucef7","\\ub098\\uc774(\\uac1c\\uc6d4)":360,"\\ucd2c\\uc601 \\uc7a5\\uc18c":"\\uc2e4\\ub0b4"},{"\\uc131\\ubcc4":"\\uc554\\ucef7","\\ub098\\uc774(\\uac1c\\uc6d4)":198,"\\ucd2c\\uc601 \\uc7a5\\uc18c":"\\uc2e4\\uc678"},{"\\uc131\\ubcc4":"\\uc218\\ucef7","\\ub098\\uc774(\\uac1c\\uc6d4)":60,"\\ucd2c\\uc601 \\uc7a5\\uc18c":"\\uc2e4\\ub0b4"},{"\\uc131\\ubcc4":"\\uc218\\ucef7","\\ub098\\uc774(\\uac1c\\uc6d4)":72,"\\ucd2c\\uc601 \\uc7a5\\uc18c":"\\uc2e4\\ub0b4"}]', 29778525: '[{"\\uc131\\ubcc4":"\\uc218\\ucef7","\\ub098\\uc774(\\uac1c\\uc6d4)":18,"\\ucd2c\\uc601 \\uc7a5\\uc18c":"\\uc2e4\\ub0b4"},{"\\uc131\\ubcc4":"\\uc218\\ucef7","\\ub098\\uc774(\\uac1c\\uc6d4)":24,"\\ucd2c\\uc601 \\uc7a5\\uc18c":"\\uc2e4\\uc678"},{"\\uc131\\ubcc4":"\\uc218\\ucef7","\\ub098\\uc774(\\uac1c\\uc6d4)":6,"\\ucd2c\\uc601 \\uc7a5\\uc18c":"\\uc2e4\\ub0b4"},{"\\uc131\\ubcc4":"\\uc554\\ucef7","\\ub098\\uc774(\\uac1c\\uc6d4)":18,"\\ucd2c\\uc601 \\uc7a5\\uc18c":"\\uc2e4\\ub0b4"}]', 29793292: '[{"\\uc131\\ubcc4":"\\uc554\\ucef7","\\ub098\\uc774(\\uac1c\\uc6d4)":120,"\\ucd2c\\uc601 \\uc7a5\\uc18c":"\\uc2e4\\ub0b4"},{"\\uc131\\ubcc4":"\\uc218\\ucef7","\\ub098\\uc774(\\uac1c\\uc6d4)":43,"\\ucd2c\\uc601 \\uc7a5\\uc18c":"\\uc2e4\\uc678"},{"\\uc131\\ubcc4":"\\uc218\\ucef7","\\ub098\\uc774(\\uac1c\\uc6d4)":77,"\\ucd2c\\uc601 \\uc7a5\\uc18c":"\\uc2e4\\ub0b4"},{"\\uc131\\ubcc4":"\\uc218\\ucef7","\\ub098\\uc774(\\uac1c\\uc6d4)":100,"\\ucd2c\\uc601 \\uc7a5\\uc18c":"\\uc2e4\\ub0b4"}]', 29810908: '[{"\\uc131\\ubcc4":"\\uc218\\ucef7","\\ub098\\uc774(\\uac1c\\uc6d4)":48,"\\ucd2c\\uc601 \\uc7a5\\uc18c":"\\uc2e4\\ub0b4"}]', 30009189: '[{"\\uc131\\ubcc4":"\\uc554\\ucef7","\\ub098\\uc774(\\uac1c\\uc6d4)":48,"\\ucd2c\\uc601 \\uc7a5\\uc18c":"\\uc2e4\\ub0b4"},{"\\uc131\\ubcc4":"\\uc218\\ucef7","\\ub098\\uc774(\\uac1c\\uc6d4)":72,"\\ucd2c\\uc601 \\uc7a5\\uc18c":"\\uc2e4\\ub0b4"},{"\\uc131\\ubcc4":"\\uc218\\ucef7","\\ub098\\uc774(\\uac1c\\uc6d4)":20,"\\ucd2c\\uc601 \\uc7a5\\uc18c":"\\uc2e4\\ub0b4"},{"\\uc131\\ubcc4":"\\uc218\\ucef7","\\ub098\\uc774(\\uac1c\\uc6d4)":144,"\\ucd2c\\uc601 \\uc7a5\\uc18c":"\\uc2e4\\ub0b4"},{"\\uc131\\ubcc4":"\\uc218\\ucef7","\\ub098\\uc774(\\uac1c\\uc6d4)":20,"\\ucd2c\\uc601 \\uc7a5\\uc18c":"\\uc2e4\\ub0b4"}]', 30091656: '[{"\\uc131\\ubcc4":"\\uc218\\ucef7","\\ub098\\uc774(\\uac1c\\uc6d4)":20,"\\ucd2c\\uc601 \\uc7a5\\uc18c":"\\uc2e4\\ub0b4"},{"\\uc131\\ubcc4":"\\uc218\\ucef7","\\ub098\\uc774(\\uac1c\\uc6d4)":144,"\\ucd2c\\uc601 \\uc7a5\\uc18c":"\\uc2e4\\uc678"},{"\\uc131\\ubcc4":"\\uc218\\ucef7","\\ub098\\uc774(\\uac1c\\uc6d4)":6,"\\ucd2c\\uc601 \\uc7a5\\uc18c":"\\uc2e4\\ub0b4"},{"\\uc131\\ubcc4":"\\uc218\\ucef7","\\ub098\\uc774(\\uac1c\\uc6d4)":2,"\\ucd2c\\uc601 \\uc7a5\\uc18c":"\\uc2e4\\uc678"},{"\\uc131\\ubcc4":"\\uc218\\ucef7","\\ub098\\uc774(\\uac1c\\uc6d4)":14,"\\ucd2c\\uc601 \\uc7a5\\uc18c":"\\uc2e4\\ub0b4"},{"\\uc131\\ubcc4":"\\uc218\\ucef7","\\ub098\\uc774(\\uac1c\\uc6d4)":20,"\\ucd2c\\uc601 \\uc7a5\\uc18c":"\\uc2e4\\ub0b4"},{"\\uc131\\ubcc4":"\\uc218\\ucef7","\\ub098\\uc774(\\uac1c\\uc6d4)":6,"\\ucd2c\\uc601 \\uc7a5\\uc18c":"\\uc2e4\\ub0b4"}]'}
Unicode JSON Data encoding using utf-8
Decoding JSON {'29704982': '[{"\\uc131\\ubcc4":"\\uc554\\ucef7","\\ub098\\uc774(\\uac1c\\uc6d4)":360,"\\ucd2c\\uc601 \\uc7a5\\uc18c":"\\uc2e4\\ub0b4"},{"\\uc131\\ubcc4":"\\uc554\\ucef7","\\ub098\\uc774(\\uac1c\\uc6d4)":198,"\\ucd2c\\uc601 \\uc7a5\\uc18c":"\\uc2e4\\uc678"},{"\\uc131\\ubcc4":"\\uc218\\ucef7","\\ub098\\uc774(\\uac1c\\uc6d4)":60,"\\ucd2c\\uc601 \\uc7a5\\uc18c":"\\uc2e4\\ub0b4"},{"\\uc131\\ubcc4":"\\uc218\\ucef7","\\ub098\\uc774(\\uac1c\\uc6d4)":72,"\\ucd2c\\uc601 \\uc7a5\\uc18c":"\\uc2e4\\ub0b4"}]', '29778525': '[{"\\uc131\\ubcc4":"\\uc218\\ucef7","\\ub098\\uc774(\\uac1c\\uc6d4)":18,"\\ucd2c\\uc601 \\uc7a5\\uc18c":"\\uc2e4\\ub0b4"},{"\\uc131\\ubcc4":"\\uc218\\ucef7","\\ub098\\uc774(\\uac1c\\uc6d4)":24,"\\ucd2c\\uc601 \\uc7a5\\uc18c":"\\uc2e4\\uc678"},{"\\uc131\\ubcc4":"\\uc218\\ucef7","\\ub098\\uc774(\\uac1c\\uc6d4)":6,"\\ucd2c\\uc601 \\uc7a5\\uc18c":"\\uc2e4\\ub0b4"},{"\\uc131\\ubcc4":"\\uc554\\ucef7","\\ub098\\uc774(\\uac1c\\uc6d4)":18,"\\ucd2c\\uc601 \\uc7a5\\uc18c":"\\uc2e4\\ub0b4"}]', '29793292': '[{"\\uc131\\ubcc4":"\\uc554\\ucef7","\\ub098\\uc774(\\uac1c\\uc6d4)":120,"\\ucd2c\\uc601 \\uc7a5\\uc18c":"\\uc2e4\\ub0b4"},{"\\uc131\\ubcc4":"\\uc218\\ucef7","\\ub098\\uc774(\\uac1c\\uc6d4)":43,"\\ucd2c\\uc601 \\uc7a5\\uc18c":"\\uc2e4\\uc678"},{"\\uc131\\ubcc4":"\\uc218\\ucef7","\\ub098\\uc774(\\uac1c\\uc6d4)":77,"\\ucd2c\\uc601 \\uc7a5\\uc18c":"\\uc2e4\\ub0b4"},{"\\uc131\\ubcc4":"\\uc218\\ucef7","\\ub098\\uc774(\\uac1c\\uc6d4)":100,"\\ucd2c\\uc601 \\uc7a5\\uc18c":"\\uc2e4\\ub0b4"}]', '29810908': '[{"\\uc131\\ubcc4":"\\uc218\\ucef7","\\ub098\\uc774(\\uac1c\\uc6d4)":48,"\\ucd2c\\uc601 \\uc7a5\\uc18c":"\\uc2e4\\ub0b4"}]', '30009189': '[{"\\uc131\\ubcc4":"\\uc554\\ucef7","\\ub098\\uc774(\\uac1c\\uc6d4)":48,"\\ucd2c\\uc601 \\uc7a5\\uc18c":"\\uc2e4\\ub0b4"},{"\\uc131\\ubcc4":"\\uc218\\ucef7","\\ub098\\uc774(\\uac1c\\uc6d4)":72,"\\ucd2c\\uc601 \\uc7a5\\uc18c":"\\uc2e4\\ub0b4"},{"\\uc131\\ubcc4":"\\uc218\\ucef7","\\ub098\\uc774(\\uac1c\\uc6d4)":20,"\\ucd2c\\uc601 \\uc7a5\\uc18c":"\\uc2e4\\ub0b4"},{"\\uc131\\ubcc4":"\\uc218\\ucef7","\\ub098\\uc774(\\uac1c\\uc6d4)":144,"\\ucd2c\\uc601 \\uc7a5\\uc18c":"\\uc2e4\\ub0b4"},{"\\uc131\\ubcc4":"\\uc218\\ucef7","\\ub098\\uc774(\\uac1c\\uc6d4)":20,"\\ucd2c\\uc601 \\uc7a5\\uc18c":"\\uc2e4\\ub0b4"}]', '30091656': '[{"\\uc131\\ubcc4":"\\uc218\\ucef7","\\ub098\\uc774(\\uac1c\\uc6d4)":20,"\\ucd2c\\uc601 \\uc7a5\\uc18c":"\\uc2e4\\ub0b4"},{"\\uc131\\ubcc4":"\\uc218\\ucef7","\\ub098\\uc774(\\uac1c\\uc6d4)":144,"\\ucd2c\\uc601 \\uc7a5\\uc18c":"\\uc2e4\\uc678"},{"\\uc131\\ubcc4":"\\uc218\\ucef7","\\ub098\\uc774(\\uac1c\\uc6d4)":6,"\\ucd2c\\uc601 \\uc7a5\\uc18c":"\\uc2e4\\ub0b4"},{"\\uc131\\ubcc4":"\\uc218\\ucef7","\\ub098\\uc774(\\uac1c\\uc6d4)":2,"\\ucd2c\\uc601 \\uc7a5\\uc18c":"\\uc2e4\\uc678"},{"\\uc131\\ubcc4":"\\uc218\\ucef7","\\ub098\\uc774(\\uac1c\\uc6d4)":14,"\\ucd2c\\uc601 \\uc7a5\\uc18c":"\\uc2e4\\ub0b4"},{"\\uc131\\ubcc4":"\\uc218\\ucef7","\\ub098\\uc774(\\uac1c\\uc6d4)":20,"\\ucd2c\\uc601 \\uc7a5\\uc18c":"\\uc2e4\\ub0b4"},{"\\uc131\\ubcc4":"\\uc218\\ucef7","\\ub098\\uc774(\\uac1c\\uc6d4)":6,"\\ucd2c\\uc601 \\uc7a5\\uc18c":"\\uc2e4\\ub0b4"}]'}
所以我已经尝试了 Stackoverflow 中的几种解决方案,如下所示,但我仍然得到与 unicode 相同的结果。
import json
with open('/content/json_test3.json', 'w', encoding='utf-8') as f:
json.dump(json_file, f, ensure_ascii=False)
import json
def json_load_byteified(file_handle):
return _byteify(
json.load(file_handle, object_hook=_byteify),
ignore_dicts=True
)
def json_loads_byteified(json_text):
return _byteify(
json.loads(json_text, object_hook=_byteify),
ignore_dicts=True
)
def _byteify(data, ignore_dicts = False):
if isinstance(data, str):
return data
# if this is a list of values, return list of byteified values
if isinstance(data, list):
return [ _byteify(item, ignore_dicts=True) for item in data ]
# if this is a dictionary, return dictionary of byteified keys and values
# but only if we haven't already byteified it
if isinstance(data, dict) and not ignore_dicts:
return {
_byteify(key, ignore_dicts=True): _byteify(value, ignore_dicts=True)
for key, value in data.items() # changed to .items() for python 2.7/3
}
# python 3 compatible duck-typing
# if this is a unicode string, return its string representation
if str(type(data)) == "<type 'unicode'>":
return data.encode('utf-8')
# if it's anything else, return it in its original form
return data
json_load_byteified(open('/content/json_test3.json'))
{'29704982': '[{"\\uc131\\ubcc4":"\\uc554\\ucef7","\\ub098\\uc774(\\uac1c\\uc6d4)":360,"\\ucd2c\\uc601 \\uc7a5\\uc18c":"\\uc2e4\\ub0b4"},{"\\uc131\\ubcc4":"\\uc554\\ucef7","\\ub098\\uc774(\\uac1c\\uc6d4)":198,"\\ucd2c\\uc601 \\uc7a5\\uc18c":"\\uc2e4\\uc678"},{"\\uc131\\ubcc4":"\\uc218\\ucef7","\\ub098\\uc774(\\uac1c\\uc6d4)":60,"\\ucd2c\\uc601 \\uc7a5\\uc18c":"\\uc2e4\\ub0b4"},{"\\uc131\\ubcc4":"\\uc218\\ucef7","\\ub098\\uc774(\\uac1c\\uc6d4)":72,"\\ucd2c\\uc601 \\uc7a5\\uc18c":"\\uc2e4\\ub0b4"}]',
'29778525': '[{"\\uc131\\ubcc4":"\\uc218\\ucef7","\\ub098\\uc774(\\uac1c\\uc6d4)":18,"\\ucd2c\\uc601 \\uc7a5\\uc18c":"\\uc2e4\\ub0b4"},{"\\uc131\\ubcc4":"\\uc218\\ucef7","\\ub098\\uc774(\\uac1c\\uc6d4)":24,"\\ucd2c\\uc601 \\uc7a5\\uc18c":"\\uc2e4\\uc678"},{"\\uc131\\ubcc4":"\\uc218\\ucef7","\\ub098\\uc774(\\uac1c\\uc6d4)":6,"\\ucd2c\\uc601 \\uc7a5\\uc18c":"\\uc2e4\\ub0b4"},{"\\uc131\\ubcc4":"\\uc554\\ucef7","\\ub098\\uc774(\\uac1c\\uc6d4)":18,"\\ucd2c\\uc601 \\uc7a5\\uc18c":"\\uc2e4\\ub0b4"}]',
'29793292': '[{"\\uc131\\ubcc4":"\\uc554\\ucef7","\\ub098\\uc774(\\uac1c\\uc6d4)":120,"\\ucd2c\\uc601 \\uc7a5\\uc18c":"\\uc2e4\\ub0b4"},{"\\uc131\\ubcc4":"\\uc218\\ucef7","\\ub098\\uc774(\\uac1c\\uc6d4)":43,"\\ucd2c\\uc601 \\uc7a5\\uc18c":"\\uc2e4\\uc678"},{"\\uc131\\ubcc4":"\\uc218\\ucef7","\\ub098\\uc774(\\uac1c\\uc6d4)":77,"\\ucd2c\\uc601 \\uc7a5\\uc18c":"\\uc2e4\\ub0b4"},{"\\uc131\\ubcc4":"\\uc218\\ucef7","\\ub098\\uc774(\\uac1c\\uc6d4)":100,"\\ucd2c\\uc601 \\uc7a5\\uc18c":"\\uc2e4\\ub0b4"}]',
'29810908': '[{"\\uc131\\ubcc4":"\\uc218\\ucef7","\\ub098\\uc774(\\uac1c\\uc6d4)":48,"\\ucd2c\\uc601 \\uc7a5\\uc18c":"\\uc2e4\\ub0b4"}]',
'30009189': '[{"\\uc131\\ubcc4":"\\uc554\\ucef7","\\ub098\\uc774(\\uac1c\\uc6d4)":48,"\\ucd2c\\uc601 \\uc7a5\\uc18c":"\\uc2e4\\ub0b4"},{"\\uc131\\ubcc4":"\\uc218\\ucef7","\\ub098\\uc774(\\uac1c\\uc6d4)":72,"\\ucd2c\\uc601 \\uc7a5\\uc18c":"\\uc2e4\\ub0b4"},{"\\uc131\\ubcc4":"\\uc218\\ucef7","\\ub098\\uc774(\\uac1c\\uc6d4)":20,"\\ucd2c\\uc601 \\uc7a5\\uc18c":"\\uc2e4\\ub0b4"},{"\\uc131\\ubcc4":"\\uc218\\ucef7","\\ub098\\uc774(\\uac1c\\uc6d4)":144,"\\ucd2c\\uc601 \\uc7a5\\uc18c":"\\uc2e4\\ub0b4"},{"\\uc131\\ubcc4":"\\uc218\\ucef7","\\ub098\\uc774(\\uac1c\\uc6d4)":20,"\\ucd2c\\uc601 \\uc7a5\\uc18c":"\\uc2e4\\ub0b4"}]',
'30091656': '[{"\\uc131\\ubcc4":"\\uc218\\ucef7","\\ub098\\uc774(\\uac1c\\uc6d4)":20,"\\ucd2c\\uc601 \\uc7a5\\uc18c":"\\uc2e4\\ub0b4"},{"\\uc131\\ubcc4":"\\uc218\\ucef7","\\ub098\\uc774(\\uac1c\\uc6d4)":144,"\\ucd2c\\uc601 \\uc7a5\\uc18c":"\\uc2e4\\uc678"},{"\\uc131\\ubcc4":"\\uc218\\ucef7","\\ub098\\uc774(\\uac1c\\uc6d4)":6,"\\ucd2c\\uc601 \\uc7a5\\uc18c":"\\uc2e4\\ub0b4"},{"\\uc131\\ubcc4":"\\uc218\\ucef7","\\ub098\\uc774(\\uac1c\\uc6d4)":2,"\\ucd2c\\uc601 \\uc7a5\\uc18c":"\\uc2e4\\uc678"},{"\\uc131\\ubcc4":"\\uc218\\ucef7","\\ub098\\uc774(\\uac1c\\uc6d4)":14,"\\ucd2c\\uc601 \\uc7a5\\uc18c":"\\uc2e4\\ub0b4"},{"\\uc131\\ubcc4":"\\uc218\\ucef7","\\ub098\\uc774(\\uac1c\\uc6d4)":20,"\\ucd2c\\uc601 \\uc7a5\\uc18c":"\\uc2e4\\ub0b4"},{"\\uc131\\ubcc4":"\\uc218\\ucef7","\\ub098\\uc774(\\uac1c\\uc6d4)":6,"\\ucd2c\\uc601 \\uc7a5\\uc18c":"\\uc2e4\\ub0b4"}]'}
我是否错误地处理了这个问题? 如果您有任何线索,请告诉我。
感谢您的时间。
您当前的代码构建了一个字典,其中的值已经是 json 个字符串。 完成此操作后,便不再可能轻松构建漂亮干净的 json 字符串。
您应该构建一个普通字典,其中键是索引值,值是表示记录的字典列表。 长话短说,只需将to_json
替换为to_dict
:
dict_file = dict(csv_file.set_index('data_id').groupby(level=0).apply(
lambda x : x.to_dict(orient = 'records')))
因为你现在得到一个普通的 Python object 由列表、dict 和(unicode 字符串)组成,它将被打印为:
{29704982: [{'나이(개월)': 360, '성별': '암컷', '촬영 장소': '실내'},
{'나이(개월)': 198, '성별': '암컷', '촬영 장소': '실외'}],
29778525: [{'나이(개월)': 18, '성별': '수컷', '촬영 장소': '실내'}],
29793292: [{'나이(개월)': 120, '성별': '암컷', '촬영 장소': '실내'},
{'나이(개월)': 43, '성별': '수컷', '촬영 장소': '실외'}],
30009189: [{'나이(개월)': 48, '성별': '암컷', '촬영 장소': '실내'},
{'나이(개월)': 72, '성별': '수컷', '촬영 장소': '실내'}],
30091656: [{'나이(개월)': 20, '성별': '수컷', '촬영 장소': '실내'},
{'나이(개월)': 144, '성별': '수컷', '촬영 장소': '실외'},
{'나이(개월)': 6, '성별': '수컷', '촬영 장소': '실내'}]}
从那以后,您可以将其转换为正确的 json 字符串或文件
json_file = json.dumps(dict_file)
给出:
{"29704982": [{"\\uc131\\ubcc4": "\\uc554\\ucef7", "\\ub098\\uc774(\\uac1c\\uc6d4)": 360, "\\ucd2c\\uc601 \\uc7a5\\uc18c": "\\uc2e4\\ub0b4"}, {"\\uc131\\ubcc4": "\\uc554\\ucef7", "\\ub098\\uc774(\\uac1c\\uc6d4)": 198, "\\ucd2c\\uc601 \\uc7a5\\uc18c": "\\uc2e4\\uc678"}], "29778525": [{"\\uc131\\ubcc4": "\\uc218\\ucef7", "\\ub098\\uc774(\\uac1c\\uc6d4)": 18, "\\ucd2c\\uc601 \\uc7a5\\uc18c": "\\uc2e4\\ub0b4"}], "29793292": [{"\\uc131\\ubcc4": "\\uc554\\ucef7", "\\ub098\\uc774(\\uac1c\\uc6d4)": 120, "\\ucd2c\\uc601 \\uc7a5\\uc18c": "\\uc2e4\\ub0b4"}, {"\\uc131\\ubcc4": "\\uc218\\ucef7", "\\ub098\\uc774(\\uac1c\\uc6d4)": 43, "\\ucd2c\\uc601 \\uc7a5\\uc18c": "\\uc2e4\\uc678"}], "30009189": [{"\\uc131\\ubcc4": "\\uc554\\ucef7", "\\ub098\\uc774(\\uac1c\\uc6d4)": 48, "\\ucd2c\\uc601 \\uc7a5\\uc18c": "\\uc2e4\\ub0b4"}, {"\\uc131\\ubcc4": "\\uc218\\ucef7", "\\ub098\\uc774(\\uac1c\\uc6d4)": 72, "\\ucd2c\\uc601 \\uc7a5\\uc18c": "\\uc2e4\\ub0b4"}], "30091656": [{"\\uc131\\ubcc4": "\\uc218\\ucef7", "\\ub098\\uc774(\\uac1c\\uc6d4)": 20, "\\ucd2c\\uc601 \\uc7a5\\uc18c": "\\uc2e4\\ub0b4"}, {"\\uc131\\ubcc4": "\\uc218\\ucef7", "\\ub098\\uc774(\\uac1c\\uc6d4)": 144, "\\ucd2c\\uc601 \\uc7a5\\uc18c": "\\uc2e4\\uc678"}, {"\\uc131\\ubcc4": "\\uc218\\ucef7", "\\ub098\\uc774(\\uac1c\\uc6d4)": 6, "\\ucd2c\\uc601 \\uc7a5\\uc18c": "\\uc2e4\\ub0b4"}]}
它可以转换回正确的 Python object 与
data = json.loads(json_file)
我在这里只使用了 json 字符串,但您可以使用带有dump
和load
的文件而不是dumps
和loads
...
对此进行测试,看看它是否修复了它
import csv
import json
def csv_to_json(csvFilePath, jsonFilePath):
jsonArray = []
#read csv file
with open(csvFilePath, encoding='utf-8') as csvf:
#load csv file data using csv library's dictionary reader
csvReader = csv.DictReader(csvf)
#convert each csv row into python dict
for row in csvReader:
#add this python dict to json array
jsonArray.append(row)
#convert python jsonArray to JSON String and write to file
with open(jsonFilePath, 'w', encoding='utf-8') as jsonf:
jsonString = json.dumps(jsonArray, indent=4)
jsonf.write(jsonString)
csvFilePath = r'data.csv'
jsonFilePath = r'data.json'
csv_to_json(csvFilePath, jsonFilePath)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.