1 回答
TA贡献1828条经验 获得超6个赞
这是问题
看起来要么你有多个子文件,要么{.....}是一个 json
要么
那些({.....})应该在一个数组中......我已经在数组方法下面展示了
a = '''{"parent": [
{"venue": "SE",
"city": "some name",
"Rating": 2,
"location": {"x": 100.0, "y": 1.0},
"pubMillis": 1581373546000}
],
"startTime": "2020-02-12 00:00:00:000",
"endTime": "2020-02-12 00:01:00:000"
}
{"parent": [
{"venue": "PP",
"city": "some name 2",
"Rating": 2,
"location": {"x": 101.0, "y": 2.0},
"pubMillis": 1581373546000}
],
"startTime": "2020-02-12 00:00:00:000",
"endTime": "2020-02-12 00:05:00:000"
}'''
a = [i.strip() if i.strip()!='}' else i.strip()+',' for i in a.split('\n') ]
a = '\n'.join(a)
a= '[\n'+a[:-1]+'\n]'
import json
a=json.loads(a)
print(a)
[{'endTime': '2020-02-12 00:01:00:000',
'parent': [{'Rating': 2,
'city': 'some name',
'location': {'x': 100.0, 'y': 1.0},
'pubMillis': 1581373546000,
'venue': 'SE'}],
'startTime': '2020-02-12 00:00:00:000'},
{'endTime': '2020-02-12 00:05:00:000',
'parent': [{'Rating': 2,
'city': 'some name 2',
'location': {'x': 101.0, 'y': 2.0},
'pubMillis': 1581373546000,
'venue': 'PP'}],
'startTime': '2020-02-12 00:00:00:000'}]
这就是您获取数据的方式
a=json.loads(a)
#print(a)
print(a[0]['parent'][0]['location'])
{'x': 100.0, 'y': 1.0}
如果你想将文件读入内存
a = "".join([i for i in open('yourFileLocation','r').readlines()])
现在你a在内存中有一个多行字符串
添加回答
举报