-1 python formatting text
我有一个具有以下格式的文件:
01*13345233000*7677082000*0335
02*1*Ground*Workshop*640.80*11.46*7344
02*2*First*Office/Labs*300.81*14.10*4241
02*3*Ground*Workshop*774.46*11.46*8875
01*13345233000*7677082000*0335
02*1*Ground*Workshop*640.80*11.46*7344
02*2*First*Office/Labs*300.81*14.10*4241
Run Code Online (Sandbox Code Playgroud)
我只想重新格式化如下(将以01开头的行复制到相应的02行):
02*1*Ground*Workshop*640.80*11.46*7344*01*13345233000*7677082000*0335
02*2*First*Office/Labs*300.81*14.10*4241*01*13345233000*7677082000*0335
02*3*Ground*Workshop*774.46*11.46*8875*01*13345233000*7677082000*0335
02*1*Ground*Workshop*640.80*11.46*734401*13345233000*7677082000*0335
02*2*First*Office/Labs*300.81*14.10*424101*13345233000*7677082000*0335
Run Code Online (Sandbox Code Playgroud)
非常感谢您的帮助.
凯特
假设您的文件被调用,textfile.txt这将创建一个列表to_output,您可以编写第二个文件或其他任何您可能需要做的事情...
to_output = []
current_01 = ""
with open('textfile.txt', 'r') as datafile:
for line in datafile:
if line.startswith("01"):
current_01 = line
elif line.startswith("02"):
to_output.append(line.strip()+"*"+current_01)
print to_output
Run Code Online (Sandbox Code Playgroud)