I am trying pretty print a JOSN data file with multiple occurrences of the following data below using the pprint function.
我正在尝试使用pprint函数打印一个带有以下数据的多次出现的JOSN数据文件。
Content of data file
数据文件的内容
{"_abcd": {"options": {}, "module": "xxxx", "crawler": "1234567890"}, "hash": 1048951621, "os": null, "ip": sgdgdfd, "isp": "ASGSFDF", "port": YYY, "hostnames": [], "location": {"city": "rtyf", "region_code": "00", "area_code": null, "longitude": 111.23344, "country_code3": "xyz", "country_name": "qazwsx", "postal_code": null, "dma_code": null, "country_code": "yy", "latitude": 1.11111}, "timestamp": "XXXXX", "domains": [], "org": "FFFFF", "data": "Unit ID: 0\n-- Slave ID Data: \t()\n\nUnit ID: 255\n-- Slave ID Data: \t()\n\n", "asn": "44444", "transport": "tcp", "ip_str": "1.2.3.4"} {"_abcd": {"options": {}, "module": "yyyy", "crawler": "999999"}, "hash": 35473835, "os": null, "ip": sgdgdfd, "isp": "TYUUF", "port": YYY, "hostnames": [], "location": {"city": "wewd", "region_code": "00", "area_code": null, "longitude": 222.3456, "country_code3": "xyz", "country_name": "qazwsx", "postal_code": null, "dma_code": null, "country_code": "yy", "latitude": 1.11111}, "timestamp": "XXXXX", "domains": [], "org": "DDDD", "data": "Unit ID: 0\n-- Slave ID Data: \t()\n\nUnit ID: 255\n-- Slave ID Data: \t()\n\n", "asn": "44444", "transport": "tcp", "ip_str": "4.3.2.1"} ...more of the same data as above
{“_ abcd”:{“options”:{},“module”:“xxxx”,“crawler”:“1234567890”},“hash”:1048951621,“os”:null,“ip”:sgdgdfd,“isp “:”ASGSFDF“,”port“:YYY,”hostnames“:[],”location“:{”city“:”rtyf“,”region_code“:”00“,”area_code“:null,”经度“: 111.23344,“country_code3”:“xyz”,“country_name”:“qazwsx”,“postal_code”:null,“dma_code”:null,“country_code”:“yy”,“latitude”:1.11111},“timestamp”:“ XXXXX“,”域“:[],”org“:”FFFFF“,”data“:”单位ID:0 \ n--从属ID数据:\ t()\ n \ n单位ID:255 \ n--从属ID数据:\ t()\ n \ n“,”asn“:”44444“,”transport“:”tcp“,”ip_str“:”1.2.3.4“} {”_ abcd“:{”options“: {},“module”:“yyyy”,“crawler”:“999999”},“hash”:35473835,“os”:null,“ip”:sgdgdfd,“isp”:“TYUUF”,“port”: YYY,“hostnames”:[],“location”:{“city”:“wewd”,“region_code”:“00”,“area_code”:null,“经度”:222.3456,“country_code3”:“xyz”, “country_name”:“qazwsx”,“postal_code”:null,“dma_code”:null,“country_code”:“yy”,“latitude”:1.11111},“timestamp”:“XXXXX”,“doma ins“:[],”org“:”DDDD“,”data“:”单位ID:0 \ n--从属ID数据:\ t()\ n \ n单位ID:255 \ n - 从属ID数据: \ t()\ n \ n“,”asn“:”44444“,”transport“:”tcp“,”ip_str“:”4.3.2.1“} ...更多与上述相同的数据
I found the code in this forum and modified it to write the output to a file:
我在这个论坛中找到了代码并修改了它以将输出写入文件:
import json
from pprint import pprint
with open('data.json') as data_file:
data = json.load(data_file)
with open('outfile.json,'w') as data_out
pprint(data,stream=data_out)
It works fine if the date file contains just 1 occurrence of the data and will fail when it has multiple occurrences.
如果日期文件只包含1次数据,则它可以正常工作,并且当它有多次出现时会失败。
How to modify the code to make it work with multiple occurrences of my data?
如何修改代码以使其适用于多次出现的数据?
1 个解决方案
#1
1
It works fine if the date file contains just 1 occurrence of the data and will fail when it has multiple occurrences.
如果日期文件只包含1次数据,则它可以正常工作,并且当它有多次出现时会失败。
If you mean you have
如果你的意思是你有
{"_abcd": ...} {"_abcd": ...} {"_abcd": ...}
That's invalid JSON. To make it valid, you wrap those objects in an array with [
at the beginning, commas in-between, and ]
at the end:
这是无效的JSON。为了使其有效,您将这些对象包装在一个数组中,其中包含[在开头,逗号在中间,以及]在结尾:
[ {"_abcd": ...}, {"_abcd": ...}, {"_abcd": ...} ]
#1
1
It works fine if the date file contains just 1 occurrence of the data and will fail when it has multiple occurrences.
如果日期文件只包含1次数据,则它可以正常工作,并且当它有多次出现时会失败。
If you mean you have
如果你的意思是你有
{"_abcd": ...} {"_abcd": ...} {"_abcd": ...}
That's invalid JSON. To make it valid, you wrap those objects in an array with [
at the beginning, commas in-between, and ]
at the end:
这是无效的JSON。为了使其有效,您将这些对象包装在一个数组中,其中包含[在开头,逗号在中间,以及]在结尾:
[ {"_abcd": ...}, {"_abcd": ...}, {"_abcd": ...} ]