filename = "dict.json"
def read_dict(filename):
file = open( script_dir + filename )
dict = json.load( file )
file.close()
return dict
def test(dict):
key = raw_input("enter key: ")
for element in dict:
while key in element:
print element
key = raw_input("key taken, try again: ")
val = raw_input("enter val: ")
new_element = {key:val}
#lets get some clear output
print new_element
print key
dict.append(new_element)
print dict
#return dict
test(read_dict(filename))
Output:
^_^ python newnor.py
enter key: både
enter val: both
{'b\xc3\xa5de': 'both'}
både
[{u'foo': u'bar'}, {u'moo': u'mar'}, {'b\xc3\xa5de': 'both'}]
The foo bar and moo mar have all ready been set by me… but what I am confused about is why when I print my key it is a normal string, but then when I create a dictionary ( new_element) it becomes the unicode funk…
I have tried playing with str() and unicode(), but no luck as of yet. Any ideas?
edit:
tried this:
import sys
def key():
k = raw_input()
k = k.decode(sys.stdin.encoding)
element = {k:"bar"}
print element
key()
termianl:
^_^ python qtest.py
både
{u'b\xe5de': 'bar'}
so at least there is a u infront of it…
Input is encoded in the system encoding. You will need to decode it before using it.