首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >(Python)将CSV文件转换为JSON

(Python)将CSV文件转换为JSON
EN

Stack Overflow用户
提问于 2022-08-06 05:04:57
回答 2查看 149关注 0票数 1

我试着把csv文件转换成Json。数据文件如下所示。

但是当我加载一个Json文件时,我看到了'\uc131‘这个unicode类型。

我的问题是,要看韩语的原文,需要做些什么?

代码语言:javascript
复制
    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    실내
代码语言:javascript
复制
#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格式的结果。

代码语言:javascript
复制
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的相同结果。

代码语言:javascript
复制
import json

with open('/content/json_test3.json', 'w', encoding='utf-8') as f:
    json.dump(json_file, f, ensure_ascii=False)
代码语言:javascript
复制
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
代码语言:javascript
复制
json_load_byteified(open('/content/json_test3.json'))
代码语言:javascript
复制
{'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"}]'}

我是不是不正确地处理这个问题?如果你有任何线索请告诉我。

谢谢您抽时间见我。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-08-06 08:13:58

您当前的代码构建了一个dict,其中的值已经是json字符串。一旦完成了这一点,就不可能轻松地构建一个漂亮而干净的json字符串。

相反,您应该构建一个普通的dict,其中键是索引值,值是代表记录的dict列表。长话短说,将to_json替换为to_dict

代码语言:javascript
复制
dict_file = dict(csv_file.set_index('data_id').groupby(level=0).apply(
    lambda x : x.to_dict(orient = 'records')))

因为您现在得到了一个普通的Python对象,它由列表、切分和(unicode字符串)组成,它将被打印为:

代码语言:javascript
复制
{29704982: [{'나이(개월)': 360, '성별': '암컷', '촬영 장소': '실내'},
            {'나이(개월)': 198, '성별': '암컷', '촬영 장소': '실외'}],
 29778525: [{'나이(개월)': 18, '성별': '수컷', '촬영 장소': '실내'}],
 29793292: [{'나이(개월)': 120, '성별': '암컷', '촬영 장소': '실내'},
            {'나이(개월)': 43, '성별': '수컷', '촬영 장소': '실외'}],
 30009189: [{'나이(개월)': 48, '성별': '암컷', '촬영 장소': '실내'},
            {'나이(개월)': 72, '성별': '수컷', '촬영 장소': '실내'}],
 30091656: [{'나이(개월)': 20, '성별': '수컷', '촬영 장소': '실내'},
            {'나이(개월)': 144, '성별': '수컷', '촬영 장소': '실외'},
            {'나이(개월)': 6, '성별': '수컷', '촬영 장소': '실내'}]}

从此,您可以将其转换为正确的json字符串或文件。

代码语言:javascript
复制
json_file = json.dumps(dict_file)

给予:

代码语言:javascript
复制
{"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对象。

代码语言:javascript
复制
data = json.loads(json_file)

这里我只使用json字符串,但是您可以使用dumpload的文件,而不是dumpsloads.

票数 1
EN

Stack Overflow用户

发布于 2022-08-06 07:39:16

用这个进行测试,看看它是否修复了它。

代码语言:javascript
复制
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)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73257317

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档