import json
with open('C:/Users/Administrator/Desktop/a.txt') as filea:
name = json.load(filea)
print('Hello, ' + name + ' !')
上面是我的代码语言 然后就报错一大堆:
JSONDecodeError
Traceback (most recent call last)
<ipython-input-47-cb98e1680220> in <module>
2
3 with open('C:/Users/Administrator/Desktop/a.txt') as filea:
----> 4 name = json.load(filea)
5 print('Hello, ' + name + ' !')
D:\Anaconda\lib\json\__init__.py in load(fp, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
294 cls=cls, object_hook=object_hook,
295 parse_float=parse_float, parse_int=parse_int,
--> 296 parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
297
298
D:\Anaconda\lib\json\__init__.py in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
346 parse_int is None and parse_float is None and
347 parse_constant is None and object_pairs_hook is None and not kw):
--> 348 return _default_decoder.decode(s)
349 if cls is None:
350 cls = JSONDecoder
D:\Anaconda\lib\json\decoder.py in decode(self, s, _w)
335
336 """
--> 337 obj, end = self.raw_decode(s, idx=_w(s, 0).end())
338 end = _w(s, end).end()
339 if end != len(s):
D:\Anaconda\lib\json\decoder.py in raw_decode(self, s, idx)
353 obj, end = self.scan_once(s, idx)
354 except StopIteration as err:
--> 355 raise JSONDecodeError("Expecting value", s, err.value) from None
356 return obj, endJSONDecodeError: Expecting value: line 1 column 1 (char 0)
3 回答
战渣渣
TA贡献12条经验 获得超3个赞
with 后面的filea是一个对象,想用json.loads出来的话,需要先把txt文本中的内容读出来
data = filea.readlines()
json.loads(data)
而不是直接load(filea),另外我印象中应该是loads不是load呀、
添加回答
举报
0/150
提交
取消