简体   繁体   中英

Split a long one line of json file into multilines

I'm trying to figure out how to split a one line JSON file into a multiliner, after every comma.

I've tried using the the split method, but it is still not the output I'm looking for. This is how far ive come so far.

with open('api.txt', 'w+') as f : 
        api = res.read().decode('utf-8')
        f.write(api)

        new_api = str(api).split(",")
        with open('new_api.txt', 'w+') as n : 
            n.write(new_api)

I would like a result like this:

line1"language":null

line2"has_issues":true line3"has_projects":true line4"has_downloads":true line5"has_wiki":true line6 "has_pages":false

You can try and adding the .join to your method. After declaring the new_api.

Like this:

with open('api.txt', 'w+') as f : 
    api = res.read().decode('utf-8')
    f.write(api)

    new_api = str(api).split(",")

    new_api = "\n".join(["".join(new_api[i:i+1]) for i in range(0,len(new_api))])

This will split you comma, and even decode your line, from the beginning to the end of the comma. Let me know if this solved your problem.

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