2016-09-24 13 views
-1

Iはデータ解析と第一実施例のためのPythonの例に沿って以下AMは示していますPythonのデータ分析のためのエラー:期待値:行1列1(CHAR 0)

In [15]: path = 'ch02/usagov_bitly_data2012-03-16-1331923249.txt' 
In [16]: open(path).readline() 
Out[16]: '{ "a": "Mozilla\\/5.0 (Windows NT 6.1; WOW64) AppleWebKit\\/535.11 
(KHTML, like Gecko) Chrome\\/17.0.963.78 Safari\\/535.11", "c": "US", "nk": 1, 
"tz": "America\\/New_York", "gr": "MA", "g": "A6qOVH", "h": "wfLQtf", "l": 
"orofrog", "al": "en-US,en;q=0.8", "hh": "1.usa.gov", "r": 
"http:\\/\\/www.facebook.com\\/l\\/7AQEFzjSi\\/1.usa.gov\\/wfLQtf", "u": 
"http:\\/\\/www.ncbi.nlm.nih.gov\\/pubmed\\/22415991", "t": 1331923247, "hc": 
1331822918, "cy": "Danvers", "ll": [ 42.576698, -70.954903 ] }\n' 

Iを得る手順に従った後:

In [21]: open(path).readline() 
Out[21]: '{ "a": "Mozilla\\/5.0 (Windows NT 6.1; WOW64) AppleWebKit\\/535.11 
(KHTML, like Gecko) Chrome\\/17.0.963.78 Safari\\/535.11", "c": "US", "nk": 1, " 
tz": "America\\/New_York", "gr": "MA", "g": "A6qOVH", "h": "wfLQtf", "l": "orofr 
og", "al": "en-US,en;q=0.8", "hh": "1.usa.gov", "r": "http:\\/\\/www.facebook.co 
m\\/l\\/7AQEFzjSi\\/1.usa.gov\\/wfLQtf", "u": "http:\\/\\/www.ncbi.nlm.nih.gov\\ 
/pubmed\\/22415991", "t": 1331923247, "hc": 1331822918, "cy": "Danvers", "ll": [ 
42.576698, -70.954903 ] }\n' 

唯一の違いは、「」部分です。

その後、私がしようとすると:

import json 
path = 'ch02/usagov_bitly_data2012-03-16-1331923249.txt' 
records = [json.loads(line) for line in open(path)] 

...私が得るものです:私はこのJSONDecodeErrorエラーを取得する理由

`In [22]: import json 

In [23]: path = 'usagov_bitly_data2012-03-16-1331923249.txt' 

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

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

C:\Anaconda3\lib\json\__init__.py in loads(s, encoding, cls, object_hook, parse_ 
float, parse_int, parse_constant, object_pairs_hook, **kw) 
    317    parse_int is None and parse_float is None and 
    318    parse_constant is None and object_pairs_hook is None and not 
kw): 
--> 319   return _default_decoder.decode(s) 
    320  if cls is None: 
    321   cls = JSONDecoder 

C:\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): 

C:\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)` 

いずれかを知っていますか?

答えて

0

JSON形式ではありませんが、あなたは異常なテキストを削除するか、オンライン形式のJSONをより明確にする必要があります。

+0

ご回答いただきありがとうございます。 ファイルをtxt形式で開くと、この異常が表示されないため、削除できません。 –

関連する問題