Windows 10 x64에서 Python 3.5.2를 사용하고 있습니다. 내가 읽는 JSON
파일은 this이며 두 개 이상의 배열을 포함하는 JSON
배열입니다.Python 3으로 JSON 파일 읽기
json
모듈을 사용하여이 JSON
파일을 구문 분석하려고합니다. docs에 설명 된대로 JSON
파일은 RFC 7159
을 준수해야합니다. 내 파일 here을 확인하고 그것이이 RFC 7159
형식과 완벽하게 괜찮지 만하려고 할 때이 간단한 파이썬 코드를 사용하여 읽기 나에게 말한다 :
with open(absolute_json_file_path, encoding='utf-8-sig') as json_file:
text = json_file.read()
json_data = json.load(json_file)
print(json_data)
나는이 예외를 받고 있어요 :
Traceback (most recent call last):
File "C:\Program Files (x86)\JetBrains\PyCharm 4.0.5\helpers\pydev\pydevd.py", line 2217, in <module>
globals = debugger.run(setup['file'], None, None)
File "C:\Program Files (x86)\JetBrains\PyCharm 4.0.5\helpers\pydev\pydevd.py", line 1643, in run
pydev_imports.execfile(file, globals, locals) # execute the script
File "C:\Program Files (x86)\JetBrains\PyCharm 4.0.5\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "C:/Users/Andres Torti/Git-Repos/MCF/Sur3D.App/shapes-json-checker.py", line 14, in <module>
json_data = json.load(json_file)
File "C:\Users\Andres Torti\AppData\Local\Programs\Python\Python35-32\lib\json\__init__.py", line 268, in load
parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
File "C:\Users\Andres Torti\AppData\Local\Programs\Python\Python35-32\lib\json\__init__.py", line 319, in loads
return _default_decoder.decode(s)
File "C:\Users\Andres Torti\AppData\Local\Programs\Python\Python35-32\lib\json\decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\Andres Torti\AppData\Local\Programs\Python\Python35-32\lib\json\decoder.py", line 357, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
을 이 정확한 파일을 Javascript에서 완벽하게 읽을 수는 있지만 파이썬으로 파싱 할 수는 없습니다. 파일에 문제가 있거나 파이썬 파서에 문제가 있습니까?
파일의 인코딩에 문제가 있습니다. 그게 무슨 가치가있어'requests.get ("http://pastebin.com/raw/Yjs6FAfm") .json()'works : –
@ cricket_007'encoding = 'utf-8- sig '파일이 UTF-8-BOM이 아니기 때문에 파일의 시작 부분에 이상한 문자가 나타나지 않도록하십시오. – Andres
맞습니다. 문제는 파일의 시작 부분에서'line 1 column 1 ' –