I am having some real trouble. I have a .txt file with contents like this:
{'x': '1', 'y': 's'}
And I would like to import this to a dict. I previously used this code, but it doesn't work:
import json
database = {}
f = open('ds.txt','r')
def load():
database=json.load(open("ds.txt"))
print(database)
load()
The errors are like this:
Traceback (most recent call last):
File "C:\Users\tan\Downloads\Compressed\project2\ct.py", line 11, in <module>
load()
File "C:\Users\tan\Downloads\Compressed\project2\ct.py", line 6, in load
database=json.load(open("ds.txt"))
File "C:\Users\tan\AppData\Local\Programs\Python\Python35-32\lib\json\__init__.py", line 268, in load
parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
File "C:\Users\tan\AppData\Local\Programs\Python\Python35-32\lib\json\__init__.py", line 319, in loads
return _default_decoder.decode(s)
File "C:\Users\tan\AppData\Local\Programs\Python\Python35-32\lib\json\decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\tan\AppData\Local\Programs\Python\Python35-32\lib\json\decoder.py", line 355, in raw_decode
obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
I also tried this, but I don't think it really works:
database={}
f=open("ds.txt","r")
def load():
o= f.read()
database=o
print(database)
load()
This piece of code belongs to a larger code, so if the whole code is required to answer my question, please tell me. Thank you.