Not getting JSON's full attribute value of a json property
我正在从本地放置在PC上的JSON文件中提取一些数据:结构是:
JSON数据样本
1 2 3 4 5 6 7 8 9 10 11 | {"totalRecords": 1, "items": [{ "name":"Services Ltd", "country":"GB", "city":"Evesham", "zip":"WR11 1BY", "address":"Northwick House Coopers Lane" }] } |
python脚本
1 2 3 4 5 6 7 8 9 10 11 12 | import json from pprint import pprint fileName = r'naturesmenu.json' with open(fileName, 'r') as f: data = json.load(f) dataData = data["items"] c = 1 for i in dataData: # pprint(i) name = i['name'] address = i['address'] print(address) |
号
为:
1 | address = i['address'] |
输出应为:
1 2 3 | Northwick House Coopers Lane |
。
或者,如果它移除/具有回车的效果,那么输出应该如下所示:
1 2 3 | Northwick House Coopers Lane |
但我只得到这个输出:
1 | Coopers Lane |
。
不是对应于
不带换行符的回车将转到行首,然后开始覆盖同一行,它不会转到下一行。因此,
1 | Coopers Laneuse |
更改JSON文件,使其包含
您可以选择
1 2 3 4 5 6 7 8 9 10 11 | dataData = data["items"] c = 1 for i in dataData: # pprint(i) name = i['name'] print(name) address = i['address'].replace(' ',' ') print(address) # 'Northwick House Coopers Lane' |
或者,如果您希望在新行中输出,则在
1 2 3 4 5 6 | address = i['address'].replace(' ',' ') # Northwick House # Coopers Lane |