Python 最好使用join还是append?
目前我有以下代码:Python 最好使用join还是append?,python,join,append,Python,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] == opcodeI
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)
输出:
1601403947013028
1 60 14039 470 13 0 28
0 60 14039 470 13 1 281 60 14039 470 13 0 28#我想把这两个分开
160140394701328线,这是错误的情况
#只有当我发现
#目标线
#满足if语句。
只需在末尾添加一个+“\n”
,如下所示
line = ' '.join(spLine) + "\n"
这将在连接后添加新行 不能将append与字符串一起使用。只需在加入后使用
+
添加一个新行即可。非常感谢!我甚至不知道我能做到这一点,欢迎你。如果您想了解python字符串的更多信息,请查看此处,