我编写了一个python程序,将以下行(大约10000个)从一个文件复制到另一个文件。条件是如果lwz中的源寄存器和目标寄存器相同则使该行加粗并复制。否则只需复制。
Line 850: lwz r3, 0(r17)
Line 851: 1004f0: 80 71 00 00 lwz r3,0(r17)
Line 1039: lwz r5,0(r3)
Line 1040: 100598: 80 a3 00 00 lwz r5,0(r3)
Line 2029: lwz r9, 0(r4) /* load half word from source */
Line 2030: 1009f8: 81 24 00 00 lwz r9,0(r4)
Line 2126: 100a5c: 81 3f 00 10 lwz r9,16(r31)
Line 2131: 100a68: 80 1f 00 10 lwz r0,16(r31)
我编写了以下程序,将行从一个文件复制到另一个文件。
import os
import sys
f1 = open('test.txt')
f2 = open('Output.txt', 'w')
str2 = "r9";
i=0
while i < 10:
Lines=f1.readline()
#Here I dont know
f2.write(str(Lines))
i=i+1;
f1.close()
f2.close()
任何人都可以建议如何将从文件读取的行转换为字符串,以便我可以对其应用字符串操作。虽然我直接申请但却给出了错误。
答案 0 :(得分:1)
您可以迭代文件中的行:
with open("test.txt","r") as f1:
with open("Output.txt","w") as f2:
for line in f1:
dosomethingwith(line)
f2.write(line)
答案 1 :(得分:1)
假设您正在使用Python3,并假设您的文件是csv格式且tabulator
符号是分隔符:
with open('text.txt', 'r', encoding='utf-8') as left_file:
with open('Output.txt', 'w', encoding='utf-8') as right_file:
for left_line, right_line in zip(left_file, right_file):
left_cols = left_line.split('\t')
right_cols = right_cols.split('\t')
if left_cols[-1] == right_cols[-1]:
# make text bold
...