简体   繁体   中英

How to convert txt file to json format with python?

i am trying to write a script that takes a text file and converts it into a json file:

the text file in question has the following contents:

Mango
800 lbs
Mango contains higher levels of vitamin C than ordinary fruits. Eating mango can also reduce cholesterol and triglycerides,and help prevent cardiovascular disease. Due to its high level of vitamins, regular consumption of mango play an important role in improving body function and moisturizing the skin.

the json file must have the following format

{"name": "Mango", "weight": 800, "description": "Mango contains higher levels of vitamin C than ordinary fruits. Eating mango can also reduce cholesterol and triglycerides,and help prevent cardiovascular disease. Due to its high level of vitamins, regular consumption of mango play an important role in improving body function and moisturizing the skin.", "image_name": "010.jpeg"}

here is my code:

import json

# the file to be converted to
# json format
filename = 'descriptions.txt'

fields = ["name", "weight", "descriptions"]

# dictionary where the lines from
# text will be stored
dict1 = {}

# creating dictionary
with open(filename) as fh:
    i = 0

    for line in fh:

        # reads each line and trims of extra the spaces
        # and gives only the valid words

        description = line.strip().split(None, 1)

        print(description)

        while i < len(fields):
            dict1[fields[i]] = description[i]
            i += 1

out_file = open("test1.json", "w")
json.dump(dict1, out_file, indent = 4, sort_keys = False)
out_file.close()

when i run the code, i am getting the error message "IndexError: list index out of range".

Another specification is that the weight field must only display the number 800 without the "lbs" part

Can someone please tell me what i did wrong?

best regards

Nicholas Monteiro Vital

Try to populate your dict like this:

dict1 = {}
fields = ["name", "weight", "descriptions"]
with open("File_new.txt") as fh:
    # you can iterate over a fields and the file's lines at the same time using zip 
    for field, line in zip(fields, fh):
        dict1[field] = line.strip() if field != "weight" else line.strip().split()[0]
print(dict1)

You initialize i = 0 before starting the for loop, but you never reset it back to 0 inside the loop. And anyway, logic of the while loop is all wrong. It would be easier to drop that loop altogether:

import json
filename = 'descriptions.txt'
fields = ["name", "weight", "descriptions"]
dict1 = {}
with open(filename) as fh:
    lines = fh.readlines()
    for i, f in enumerate(fields):
        dict1[f] = lines[i].strip()
    dict1['weight'] = dict1['weight'].split()[0] 

with open("test1.json", "w") as out_file:
   json.dump(dict1, out_file, indent = 4, sort_keys = False)

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