0
我想要從文本文件JSON,並作出['ids']
的SyntaxError同時考取JSON
{"previous_cursor": 0, "previous_cursor_str": "0", "next_cursor": 1351473067174597097, "ids": [250718906, 66612533], "next_cursor_str": "1351473067174597097"} {"previous_cursor": -1351352880715992572, "previous_cursor_str": "-1351352880715992572", "next_cursor": 0, "ids": [113030689, 22020972], "next_cursor_str": "0"}
我的代碼列表值
import json
f = open('22580877','r')
data = f.read()
datalist = data.split('\n')
idslist = []
for i in datalist:
datadic = eval(i)
print(datadic['ids'])
idslist.extend(datadic['ids'])
datadic = {}
for j in idslist:
print(j)
f.close()
錯誤味精是
Traceback (most recent call last):
File "test.py", line 11, in <module>
datadic = eval(i)
File "<string>", line 0
^
SyntaxError: unexpected EOF while parsing
我在代碼中找不到我的語法錯誤。請幫幫我!
你爲什麼使用'eval'來解析JSON?爲什麼不是實際的'json'庫?爲什麼你在新行上分裂而不是將整個事情傳遞給'json.loads'? –
使用eval和exec是不好的做法,你應該儘量避免它 –
SyntaxError不在你的代碼中,它在你傳遞給'eval'的'datalist'的字符串中。但正如丹尼爾所說,你不應該爲此使用'eval'。使用'json'模塊功能。一般應避免使用「eval」和「exec」,因爲它們可能存在安全風險。有關詳情,請參閱SO老將Ned Batchelder的[Eval really dangerous](http://edbatchelder.com/blog/201206/eval_really_is_dangerous.html)。 –