我正在生成一个文件文本,如下所示,并且我试图在生成该文件文本后取消显示该文件文本的最后一个字符“,”但我无法...
*Nset, nset
0570, 0571, 0572, 0573, 0574, 0575, 0576, 0577, 0578, 0579, 0579
0580, 0581, 0582, 0583, 0584, 0585, 0586, 0587, 0588, 0589, 0589
0590, 0591,
我在Stack上发现了这种代码,但是,它不起作用...
import sys
with open(sys.argv[1]) as f, open('out.txt', 'w') as out:
for line in f:
out.write(line.split()[-1]+'\n')
----编辑后----
我的代码来生成文件文本:
fichier_write.write("*Nset,")
fichier_write.write("\n")
compt = 0
for i in range(0,node_label.size):
if Eleme_CI[i]!=0:
fichier_write.write("%.4i, "%(Eleme_CI[i])),
compt = compt + 1
if compt == 10:
fichier_write.write("%.4i"%(Eleme_CI[i])),
fichier_write.write("\n")
compt = 0
fichier_write.close()
最佳答案
import re
# get text into a string \Z is the end of a string in regex
with open('data') as f:
text = f.read()
text = re.sub(r'.\Z',r'',text)
print(text)
Money amount left 39.0
*Nset, nset
0570, 0571, 0572, 0573, 0574, 0575, 0576, 0577, 0578, 0579, 0579
0580, 0581, 0582, 0583, 0584, 0585, 0586, 0587, 0588, 0589, 0589
0590, 0591