1

I have a dictionary in a text file in exact format given bellow,

 {'one': 'a', 'two': 'b', 'three': 'c'}

I want to take that dictionary in a variable say dict1. My program generates new key value pairs, if the new generated key is is not present in dict1 then I want to add this new key in dict1. finally I want to update txt file with this updated dictionary.

I know how to add key value pairs to a dict but I am not able to read that dictionary from txt file into a dictionary type of variable.

can anybody help plz ?

Ritzor
  • 665
  • 7
  • 26
  • something like http://stackoverflow.com/questions/3277503/python-read-file-line-by-line-into-array is how to read a text file – depperm Dec 17 '15 at 17:29
  • "I am not able to read that dictionary from txt file into a dictionary type of variable" <- why, what's the problem? – timgeb Dec 17 '15 at 17:35
  • Here's a hint: use [`ast.literal_eval`.](https://docs.python.org/2/library/ast.html#ast.literal_eval) – wkl Dec 17 '15 at 17:37
  • alternatively, store and load as json. – timgeb Dec 17 '15 at 17:39
  • How was this file written in the first place? You need to know the rules used for serialization before you can figure out how to parse it again. – tdelaney Dec 17 '15 at 17:43
  • initially the file is handwritten, any newly encountered key later I want to append to that dictionary.. What is good practice should I use dictionary or a json to store those key value pairs? I just want to store key value pairs in a text file, this text file will be given as input to another program which will generate some keys if key already not present in txt file then I need to append this new key with some value to the text file.. – Ritzor Dec 17 '15 at 18:32
  • SO whats good practice? should I store in the form of json or a dictionary? – Ritzor Dec 17 '15 at 18:32
  • That's up to you, really - if you want this to be editable without being loaded into your code, then I'd pick a format like JSON. It also makes it easier to process beyond just Python. If this isn't going to be of much use beyond your work, then it doesn't really matter. – wkl Dec 17 '15 at 18:45

2 Answers2

2

This piece of code works for me:

import ast

dict_file = open("dict.txt", "r")
dict_string = dict_file.readline().strip()
dict_file.close()

d = ast.literal_eval(dict_string)
print d["one"]

#change your dictionary e.g:
d["foo"] = "bar"

f = open("dict.txt", "w")
f.write(str(d))
f.close()

It simply reads the string from the file and creates a dictionary using the "ast.literal_eval". You can then commit changes to the dictionary, convert it to a string and write it to the txt file.

You could alternatively use Pickle

FooBar
  • 334
  • 2
  • 10
1

If your text file is named foo.txt, then

import json

with open('foo.txt', 'r') as f:
    d = json.load(f)

new_key = 'foo'
if new_key not in d:
    d[new_key] = 'bar'
    with open('foo.txt', 'w') as f:
        json.dump(d, f)
JCVanHamme
  • 1,050
  • 7
  • 12
  • If his file is formatted as he's shown, your code's going to throw an exception because of the single quotes. He'll need `"` everywhere to make it JSON-compatible. – wkl Dec 17 '15 at 17:43
  • OP didn't show us JSON so this can't really be considered an answer. – tdelaney Dec 17 '15 at 17:43