【发布时间】:2017-10-15 11:01:10
【问题描述】:
我尝试使用此代码在 pandas 中加载 JSON 文件
我在第一行就有这个错误。我认为在 json 结构中同样的事情是错误的,因为我也尝试过 pd.read_json 并且它没有工作。这里有什么问题?
Name_file='data582750.txt'
l=[]
with open(Name_file) as f:
for line in f:
l.append(line)
data = json.loads(l)
json_normalize(data)
我收到此错误:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-64-c050037c7cb9> in <module>()
----> 1 data = json.loads(l)
C:\Users\demighaa\AppData\Local\Continuum\Anaconda2\lib\json\__init__.pyc in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
337 parse_int is None and parse_float is None and
338 parse_constant is None and object_pairs_hook is None and not kw):
--> 339 return _default_decoder.decode(s)
340 if cls is None:
341 cls = JSONDecoder
C:\Users\demighaa\AppData\Local\Continuum\Anaconda2\lib\json\decoder.pyc in decode(self, s, _w)
362
363 """
--> 364 obj, end = self.raw_decode(s, idx=_w(s, 0).end())
365 end = _w(s, end).end()
366 if end != len(s):
TypeError: expected string or buffer
文件的第一行是:
'{"Operator":0,"Device":"F0FCC","Time":1494854190,"FormattedTime":"2017-05-15 15:16:30 +0200 CEST","Data":"042911e3c78f2193262c58b2","JSONDecodedData":"{"Service":{"S":{"Typ":0,"Version":2,"Vdd":0},"Cpt":2,"Start":0},"ExtTemperature":[22.25,21.5,21.5,21.5,21.5,22],"Moisture":[50,50,49,49,49,50],"IntTemperature":[0,0,0,0,0,0,0,0,0]}","Snr":"48.21","ComputedLocation":{"Lat":0,"Lng":0},"LinkQuality":"EXCELLENT"}\n'
【问题讨论】:
-
不要将代码作为图片发布 - 在您的帖子中添加 minimal reproducible example,人们可以复制出来(作为文本)以检查和编辑。
-
删除图像和添加文本的道具,但您应该尝试正确格式化代码以获得帮助 - 很难看到这里发生了什么。
-
您的问题不可读...您应该花时间编辑您发布的内容以改善缩进!只需一个简单的按钮
{}即可自动缩进....