我正在尝试阅读以下Json文件,然后将其粘贴到csv文件中。 该文件直接来自Google Cloud bigquery(出于某些原因,csv导出不起作用)。运行此代码时,我收到上述错误:
import json
#prompt the user for a file to import
filename = "C:/Users/abc/Documents/Python Scripts/news_results.json"
#Read JSON data into the datastore variable
with open(filename) as f:
datastore = json.load(f)
Json文件的结构由文章文本,类别和置信度组成,其中文章文本包含全文新闻文章。 :
[
{
"article_text": "Intel unveils 's photonics technology lab. Writing in the journal Nature,
Dr Paniccia - uter chips,\" he said.\n",
"category": "/Computers & Electronics/Computer Hardware/Computer Components",
"confidence": "0.5"
},
{
"article_text": "Rank 'set to by gaming fans.\n",
"category": "/Computers & Electronics/Consumer Electronics/Game Systems & Consoles",
"confidence": "0.8399999737739563"
}
]
完整的追溯:
JSONDecodeError Traceback (most recent call last)
<ipython-input-9-8fd516f33e36> in <module>()
6 #Read JSON data into the datastore variable
7 with open(filename) as f:
----> 8 datastore = json.load(f)
~\AppData\Local\Continuum\anaconda3\lib\json\__init__.py in load(fp, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
297 cls=cls, object_hook=object_hook,
298 parse_float=parse_float, parse_int=parse_int,
--> 299 parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
300
301
~\AppData\Local\Continuum\anaconda3\lib\json\__init__.py in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
352 parse_int is None and parse_float is None and
353 parse_constant is None and object_pairs_hook is None and not kw):
--> 354 return _default_decoder.decode(s)
355 if cls is None:
356 cls = JSONDecoder
~\AppData\Local\Continuum\anaconda3\lib\json\decoder.py in decode(self, s, _w)
337
338 """
--> 339 obj, end = self.raw_decode(s, idx=_w(s, 0).end())
340 end = _w(s, end).end()
341 if end != len(s):
~\AppData\Local\Continuum\anaconda3\lib\json\decoder.py in raw_decode(self, s, idx)
355 obj, end = self.scan_once(s, idx)
356 except StopIteration as err:
--> 357 raise JSONDecodeError("Expecting value", s, err.value) from None
358 return obj, end
JSONDecodeError: Expecting value: line 1 column 1 (char 0)
编辑:使用Anaconda Spyder,我得到以下回溯:
Traceback (most recent call last):
File "<ipython-input-2-3cca112a7d99>", line 1, in <module>
runfile('C:/Users/abc/Documents/Python Scripts/Jsonimport.py', wdir='C:/Users/tmeyer/Documents/Python Scripts')
File "C:\Users\abc\AppData\Local\Continuum\anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 705, in runfile
execfile(filename, namespace)
File "C:\Users\abc\AppData\Local\Continuum\anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/abc/Documents/Python Scripts/Jsonimport.py", line 15, in <module>
datastore = json.load(f)
File "C:\Users\abc\AppData\Local\Continuum\anaconda3\lib\json\__init__.py", line 299, in load
parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
File "C:\Users\abc\AppData\Local\Continuum\anaconda3\lib\json\__init__.py", line 354, in loads
return _default_decoder.decode(s)
File "C:\Users\abc\AppData\Local\Continuum\anaconda3\lib\json\decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\abc\AppData\Local\Continuum\anaconda3\lib\json\decoder.py", line 357, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
JSONDecodeError: Expecting value