# Get lines of text file.
with open('flattened_xml.txt') as file:
data = file.read().splitlines()
# Determine what fiels are present.
fields = []
for line in data:
id, _ = line.split(',')
if id not in fields:
fields.append(id)
table = [{field:field for field in fields}] # Header row.
row = {}
for line in data:
field, value = line.split(',')
if field == fields[0]:
row = {field: value}
row.update({key: None for key in fields[1:]})
continue
row.update({key: value if key == field else None for key in fields[1:]})
table.append(row)
row = {key: None for key in fields}
for row in table:
s = [f'{value if value is not None else "":11}' for value in row.values()]
print('|' + '|'.join(s) + '|')
# Get lines of text file.
with open('flattened_xml.txt') as file:
data = file.read().splitlines()
# Determine what fiels are present.
fields = []
for line in data:
id, _ = line.split(',')
if id not in fields:
fields.append(id)
table = [{field:field for field in fields}] # Header row.
row = {}
for line in data:
field, value = line.split(',')
if field == fields[0]:
row = {field: value}
row.update({key: None for key in fields[1:]})
continue
row.update({key: value if key == field else None for key in fields[1:]})
table.append(row)
row = {key: None for key in fields}
for row in table:
s = [f'{value if value is not None else "":11}' for value in row.values()]
print('|' + '|'.join(s) + '|')
发布评论
评论(2)
您提到您的现有字段位于另一个文件中。如果您可以使用它们来制作命名空白字符向量,如下所示:
那么您可以执行以下操作:
再次强调,此处不使用外部包。
You mention you have your existing fields in another file. Provided you can use these to make a named blank character vector like this:
Then you can do:
Again, no external packages are used here.
这是一种用 Python 实现的方法。
假设输入文件
'flattened_xml.txt'
包含:代码:
结果:
Here's a way to do it Python.
Assuming the input file
'flattened_xml.txt'
contains:Code:
Result: