hyp*_*ics 5 python parsing json multiline
我试图json在Python 2.7中使用库解析JSON多行文件.简化的示例文件如下:
{
"observations": {
"notice": [
{
"copyright": "Copyright Commonwealth of Australia 2015, Bureau of Meteorology. For more information see: http://www.bom.gov.au/other/copyright.shtml http://www.bom.gov.au/other/disclaimer.shtml",
"copyright_url": "http://www.bom.gov.au/other/copyright.shtml",
"disclaimer_url": "http://www.bom.gov.au/other/disclaimer.shtml",
"feedback_url": "http://www.bom.gov.au/other/feedback"
}
]
}
}
Run Code Online (Sandbox Code Playgroud)
我的代码如下:
import json
with open('test.json', 'r') as jsonFile:
for jf in jsonFile:
jf = jf.replace('\n', '')
jf = jf.strip()
weatherData = json.loads(jf)
print weatherData
Run Code Online (Sandbox Code Playgroud)
不过,我收到如下错误:
Traceback (most recent call last):
File "test.py", line 8, in <module>
weatherData = json.loads(jf)
File "/home/usr/anaconda2/lib/python2.7/json/__init__.py", line 339, in loads
return _default_decoder.decode(s)
File "/home/usr/anaconda2/lib/python2.7/json/decoder.py", line 364, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/home/usr/anaconda2/lib/python2.7/json/decoder.py", line 380, in raw_decode
obj, end = self.scan_once(s, idx)
ValueError: Expecting object: line 1 column 1 (char 0)
Run Code Online (Sandbox Code Playgroud)
只是做一些测试,我修改了代码,以便在删除换行符并去掉前导和尾随空格后,我将内容写入另一个文件(带json扩展名).令人惊讶的是,当我读回后一个文件时,我没有收到任何错误,解析成功.修改后的代码如下:
import json
filewrite = open('out.json', 'w+')
with open('test.json', 'r') as jsonFile:
for jf in jsonFile:
jf = jf.replace('\n', '')
jf = jf.strip()
filewrite.write(jf)
filewrite.close()
with open('out.json', 'r') as newJsonFile:
for line in newJsonFile:
weatherData = json.loads(line)
print weatherData
Run Code Online (Sandbox Code Playgroud)
输出如下:
{u'observations': {u'notice': [{u'copyright_url': u'http://www.bom.gov.au/other/copyright.shtml', u'disclaimer_url': u'http://www.bom.gov.au/other/disclaimer.shtml', u'copyright': u'Copyright Commonwealth of Australia 2015, Bureau of Meteorology. For more information see: http://www.bom.gov.au/other/copyright.shtml http://www.bom.gov.au/other/disclaimer.shtml', u'feedback_url': u'http://www.bom.gov.au/other/feedback'}]}}
Run Code Online (Sandbox Code Playgroud)
在使用json库之前删除新行和空格时可能会发生什么?
如果您尝试逐行解析json文件,您将会发疯。json模块具有帮助程序方法来直接读取文件对象或字符串,即loadand loads方法。 load接受包含json数据的文件的文件对象(如下所示),而loads接受包含json数据的字符串。
选项1:- 首选
import json
with open('test.json', 'r') as jf:
weatherData = json.load(jf)
print weatherData
Run Code Online (Sandbox Code Playgroud)
选项2:
import json
with open('test.json', 'r') as jf:
weatherData = json.loads(jf.read())
print weatherData
Run Code Online (Sandbox Code Playgroud)
如果您正在寻找更高性能的json解析,请查看ujson
在第一个代码段中,您尝试逐行解析它.你应该立刻解析它.最简单的是使用json.load(jsonfile).(jf变量名称具有误导性,因为它是一个字符串).所以解析它的正确方法:
import json
with open('test.json', 'r') as jsonFile:
weatherData = json.loads(jsonFile)
Run Code Online (Sandbox Code Playgroud)
尽管将json存储在一行中是个好主意,因为它更简洁.
在第二个片段中,您的问题是您将其打印为unicode字符串,这是u'string here'特定于python的.有效的json使用双引号
| 归档时间: |
|
| 查看次数: |
7890 次 |
| 最近记录: |