+ 12

Trouble editing json data with python.

so, in this code ↓ https://code.sololearn.com/cy12fVMb0A2a/#py i keep getting this error when ever i try to load the data from the json file (included in the comments off the code). i haven't been able to replicate the error on here. what i'm really trying to do is figure out why it happens, and how to prevent it. the error is also in the code comments.

21st Feb 2019, 8:22 PM
LONGTIE👔
LONGTIE👔 - avatar
11 Respuestas
+ 6
You have a typo in loads() too and no need to convert to str These two line changes and it works y = json.loads(x) print(y) https://code.sololearn.com/cx38vd1t09Xp/?ref=app
21st Feb 2019, 8:53 PM
Tibor Santa
Tibor Santa - avatar
+ 9
Tibor Santa what's the difference between the load and the loads function?
21st Feb 2019, 8:56 PM
LONGTIE👔
LONGTIE👔 - avatar
+ 7
Hm I was kinda cheating because I followed w3s tutorial, I am by no means an expert in this. https://www.w3schools.com/JUMP_LINK__&&__python__&&__JUMP_LINK/python_json.asp But looking up the standard doc, actually it seems correct to use load() with files and loads() with strings as data source. Well on SL it will only work with string.
21st Feb 2019, 9:00 PM
Tibor Santa
Tibor Santa - avatar
+ 4
So maybe the source of the error is wrong Syntax in the source file. I am not really sure how sensitive this thing is to Whitespaces and line breaks, maybe there must be some in specific position...
21st Feb 2019, 9:03 PM
Tibor Santa
Tibor Santa - avatar
+ 4
Tibor Santa, I tried your code and it worked. thanks for your help.
21st Feb 2019, 11:59 PM
LONGTIE👔
LONGTIE👔 - avatar
+ 3
Marcos Rocha then I get a TypeError
23rd Feb 2019, 4:49 PM
LONGTIE👔
LONGTIE👔 - avatar
+ 2
Your script writes crap in your json while because of the truncate function. And it throws the error when it tries to read it. It works at the first time before the write function is executed.
21st Feb 2019, 8:56 PM
Toni Isotalo
Toni Isotalo - avatar
+ 2
hello made some changes here is the fix. https://code.sololearn.com/c6aEoQRAZwwo/#py
23rd Feb 2019, 8:41 PM
akpos2016
akpos2016 - avatar
0
try using json.loads(x)
23rd Feb 2019, 4:18 PM
Marcos Rocha
Marcos Rocha - avatar
- 1
13764557060
23rd Feb 2019, 1:27 AM
Vicky
- 1
Mohamed
23rd Feb 2019, 8:02 PM
Mohamed Jabloun
Mohamed Jabloun - avatar