В настоящее время у меня есть этот код:Лучше всего использовать соединение или добавить?
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.
Вы не можете использовать append со строкой. Просто добавьте новую строку после объединения с помощью '+'. –