简体   繁体   English

值错误:期望值

[英]Value Error: Expecting Value

I have a line of code from a Data Analysis Book using python. 我从使用Python的数据分析书中获得了一行代码。

return = [json.loads(line) for line in open(path)]

Path being the designated text file I have with JSON formatted text inside. 路径是我拥有的带有JSON格式文本的指定文本文件。

The link is here. 链接在这里。 https://raw.githubusercontent.com/pydata/pydata-book/master/ch02/usagov_bitly_data2012-03-16-1331923249.txt https://raw.githubusercontent.com/pydata/pydata-book/master/ch02/usagov_bitly_data2012-03-16-1331923249.txt

ValueError                                Traceback (most recent call last)
<ipython-input-35-b1e0b494454a> in <module>()
----> 1 records = [json.loads(line) for line in open(path)]

<ipython-input-35-b1e0b494454a> in <listcomp>(.0)
----> 1 records = [json.loads(line) for line in open(path)]

/usr/lib/python3.4/json/__init__.py in loads(s, encoding, cls,    object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
316             parse_int is None and parse_float is None and
317             parse_constant is None and object_pairs_hook is None and not kw):
--> 318         return _default_decoder.decode(s)
319     if cls is None:
320         cls = JSONDecoder

/usr/lib/python3.4/json/decoder.py in decode(self, s, _w)
341 
342         """
--> 343         obj, end = self.raw_decode(s, idx=_w(s, 0).end())
344         end = _w(s, end).end()
345         if end != len(s):

/usr/lib/python3.4/json/decoder.py in raw_decode(self, s, idx)
359             obj, end = self.scan_once(s, idx)
360         except StopIteration as err:
--> 361             raise ValueError(errmsg("Expecting value", s, err.value)) from None
362         return obj, end

ValueError: Expecting value: line 2 column 1 (char 1)

Any help is appreciated. 任何帮助表示赞赏。

as said in your trace back the value being returned is the None type. 正如您在追溯中所说的,返回的值是None类型。 Null, theres nothing to return. 无,没有任何回报。 Double check your source. 仔细检查您的来源。

line 2 starts with 第2行以

"a": "GoogleMaps\/RochesterNY",

Line 1 and the other lines start with 第1行,其他行以

"a": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.78 Safari/535.11", “ a”:“ Mozilla / 5.0(Windows NT 6.1; WOW64)AppleWebKit / 535.11(KHTML,例如Gecko)Chrome / 17.0.963.78 Safari / 535.11”,

It appears that the "a" key is supposed to be an indicator of the browser type and not the URL description. 看来“ a”键应该是浏览器类型的指示符,而不是URL描述。 This would probably cause a problem when you are trying to decode it. 当您尝试对其进行解码时,这可能会导致问题。 I would suggest removing it from the data and seeing what happens. 我建议将其从数据中删除,然后看看会发生什么。

Your call to 您打给

--> 343         obj, end = self.raw_decode(s, idx=_w(s, 0).end())

appears to be in error. 似乎有误。 Did you write your own version of raw_decode() as the Python JSON manual]( https://docs.python.org/3/library/json.html ) states that 您是否按照Python JSON手册编写了自己的raw_decode()版本]( https://docs.python.org/3/library/json.html )指出

raw_decode(s) raw_decode

Decode a JSON document from s (a str beginning with a JSON document) and return a 2-tuple of the Python representation and the index in s where the document ended. 从s解码一个JSON文档(以JSON文档开头的str),并返回2个元组的Python表示形式和s中该文档结束处的索引。

This can be used to decode a JSON document from a string that may have extraneous data at the end. 这可用于从结尾可能有无关数据的字符串中解码JSON文档。

which means that ext is not a valid argument for the standard function. 这意味着ext不是标准函数的有效参数。 It is better to write your own function with a different name in order to prevent accidents in usage. 最好使用其他名称编写自己的函数,以防止使用中发生意外。

Similarly you would be better off writing your for loop 同样,最好编写for循环

records = [json.loads(line) for line in open(path)]

as for line in open(path): records = [json.loads(line) 至于open(path)中的行:records = [json.loads(line)

However, given that it appears that the exception is raised from the raw_decode, it would appear that the line that you are trying to decode contains an error. 但是,鉴于似乎是从raw_decode引发了异常,因此您似乎要解码的行似乎包含一个错误。 You should check the data that you are processing and handle the error or fix the data. 您应该检查正在处理的数据并处理错误或修复数据。 Since it is a JSON line, you should be able to print the line to determine what the error is. 由于它是JSON行,因此您应该能够打印该行以确定错误所在。 It could be your code or the actual data line. 可能是您的代码或实际的数据行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM