Skip to content Skip to sidebar Skip to footer

I Can Not Transform A File To A Dictionary In Python

I am trying to transform a file to dictionary but having error. def txt_to_dict(): dict = {} with open('GEO_human.gaf') as f: for line in f: (key, val)

Solution 1:

Use Json library:

To save dict into file

withopen("your_file") as f:
    f.write(json.dumps(your_dict))

To load dict from file

withopen("your_file") as f:
    d = json.load(f)

Solution 2:

Probably there are more than one ':' in a line. You can try:

(key, val) = line.split(":", 1)

this code splits the line at the first ':'.

Post a Comment for "I Can Not Transform A File To A Dictionary In Python"