我有 2 个示例 JSON 记录,我想使用 python 删除缩进。 使用这种语法(line.replace(“”,“”))我删除了所有缩进。 但问题是所有记录都是一张记录。 期望是每条记录都应该排成一行。 请指导我
{
"device_model": "TITAN",
"enb_locan": {
"coordinates": [
-900011,
900.87
],
"crs": {
"properties": {
"name": "EPSG:4326"
},
"type": "name"
},
"type": "Point"
},
"enb_order_distance": 25,
"trans_dt": "2024-01-11"
}
{
"device_model": "TITAN2222",
"enb_locan": {
"coordinates": [
-900011,
900.87
],
"crs": {
"properties": {
"name": "EPSG:4326"
},
"type": "name"
},
"type": "Point"
},
"enb_order_distance": 29,
"trans_dt": "2024-01-11"
}
代码使用
def process(self, inputStream, outputStream):
text = IOUtils.readLines(inputStream, StandardCharsets.UTF_8)
for line in text:
output = line.replace(" ", "")
outputStream.write(output)
听起来您想要 JSON Lines 格式,其中每个完整的 JSON 记录都位于以换行符结尾的单行上。
下面是一个粗略的嵌套花括号计数器,用于收集完整的记录并重写为单行:
import io
import json
import sys
data = '''\
{
"device_model": "TITAN",
"enb_locan": {
"coordinates": [
-900011,
900.87
],
"crs": {
"properties": {
"name": "EPSG:4326"
},
"type": "name"
},
"type": "Point"
},
"enb_order_distance": 25,
"trans_dt": "2024-01-11"
}
{
"device_model": "TITAN2222",
"enb_locan": {
"coordinates": [
-900011,
900.87
],
"crs": {
"properties": {
"name": "EPSG:4326"
},
"type": "name"
},
"type": "Point"
},
"enb_order_distance": 29,
"trans_dt": "2024-01-11"
}
'''
# Could read from file as well.
with io.StringIO(data) as f:
record = []
nested = 0
while c := f.read(1): # read and store characters
record.append(c)
if c == '{': # keep track of nesting
nested += 1
if c == '}':
nested -= 1
if nested == 0: # Once nested returns to zero, we have a full record
data = ''.join(record).strip() # build record and remove whitespace before and after
if data: # load the record and dump again without indentation
print(json.dumps(json.loads(data)), file=sys.stdout) # could print to a file.
record = []
输出:
{"device_model": "TITAN", "enb_locan": {"coordinates": [-900011, 900.87], "crs": {"properties": {"name": "EPSG:4326"}, "type": "name"}, "type": "Point"}, "enb_order_distance": 25, "trans_dt": "2024-01-11"}
{"device_model": "TITAN2222", "enb_locan": {"coordinates": [-900011, 900.87], "crs": {"properties": {"name": "EPSG:4326"}, "type": "name"}, "type": "Point"}, "enb_order_distance": 29, "trans_dt": "2024-01-11"}