gpt4 book ai didi

Python-删除文本文件中的最后一个字符

转载 作者:行者123 更新时间:2023-11-30 23:04:51 25 4
gpt4 key购买 nike

我正在生成一个文件文本,如下所示,我试图在生成后抑制该文件文本的最后一个字符“,”,但我不能...

*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

关于Python-删除文本文件中的最后一个字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33506810/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com