我正在尝试拆分inputfile.txt中的数据并写入Outputfile.txt
inputfile.txt
LIST : 2
a0 n
a3
LIST : 2
a0
a9 k
LIST : 2
a3
a5
a6 l
a8
LIST : 2
a4
a5
a6
a8
Outputfile.txt
LIST 0 : 3 9
LIST 3 : 5 6 8
LIST 4 : 5 6 8
我是python新手,我尝试过,但没有成功
def main():
updatedData = ""
with open('inputfile.txt', 'r') as f:
for line in f:
#print(line)
test = line.split(" ",1)[0][1:]+ '\t'
updatedData += test.replace('\n',' ')
要求有点模糊(根是否应该排序?),但这里有一个工作示例(至少对于op中的输入文件和输出),我包括了一些注释来解释算法是如何工作的
如果需要的话,在打印前进行分类很容易
编辑以处理多数字键。
编辑以不合并重复键:
如果我们不需要组合键,代码会更简单。我们根本不需要做任何全球跟踪。每个列表块都是独立的。您只需收集节点,直到到达块的末尾
相关问题 更多 >
编程相关推荐