目前我有这样的代码:最好使用join或append?
with open(w_file, 'r') as file_r:
for line in file_r:
if len(line) > 0:
spLine = line.split()
if(spLine[0] == operandID and spLine[1] == bitID
and spLine[3] == sInstID and spLine[4] == opcodeID):
spLine[-2]='1'
line = ' '.join(spLine) # I need to add a new line into
# "line" after join all the spLine
lines.append(line)
print line
with open(w_file, 'w') as file_w:
for line in lines:
file_w.write(line)
输出:
1 60 14039 470 13 0 28
1 60 14039 470 13 0 28
0 60 14039 470 13 1 281 60 14039 470 13 0 28 # I want to separate these two
1 60 14039 470 13 0 28 # lines, this wrong situation
# only happens when I found
# the target line which
# satisfies the if statement.
您不能使用附加字符串。只需简单地在使用'+'加入后添加换行符。 –