简体   繁体   中英

(Python) Convert CSV file to JSON

I've tried to convert a csv file to Json. The dataframe looks like below.

But when I loaded a Json file, I see '성' this unicode type.

My question is, What needs to be done to see the original Korean words?

    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))

After running this code, I still get the result of unicode format.

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"}]'}


So I have tried several solutions from Stackoverflow like below, but I still get the same result of 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"}]'}

Am I approaching this problem incorrectly? Please let me know if you have any clue.

Thank you for your time.

Your current code builds a dict where the values are already json strings. Once this has been done, it is no longer possible to easily build a nice and clean json string.

You should instead build a plain dict where the keys are the index values, and the values are lists of dicts representing the records. Long story short, just replace to_json with to_dict :

dict_file = dict(csv_file.set_index('data_id').groupby(level=0).apply(
    lambda x : x.to_dict(orient = 'records')))

Because you now get a plain Python object composed of lists, dicts and (unicode strings) which will be pprinted as:

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

From that on, you can convert it to a correct json string or file

json_file = json.dumps(dict_file)

gives:

{"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"}]}

And it can be converted back to correct Python object with

data = json.loads(json_file)

I only used a json string here, but you could use a file with dump and load instead of dumps and loads ...

Test with this and see if it fixes it

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM