with open ('Exporting_data.txt', 'w+') as fp:
fp.write(data_Str)
fp.write('\n'+ labels_Str)
fin = open("Exporting_data.txt", "rt")
fout = open("Exporting_data_new.txt", "wt")
for line in fin:
fout.write(line.replace('preds', ''))
fin.close()
fout.close()
I did this to remove the very first word of my text file (preds) but now it leaves a white space that I don't know how to remove. The text file now looks like that.
B-Product B-Product B-Product B-Product
from originally,
preds B-Product B-Product B-Product B-Product
您可以使用lstrip
去除前导空格:
fout.write(line.replace('preds', '').lstrip())
Use .lstrip()
+fix indentation+use with
:
with open ('Exporting_data.txt', 'w+') as fp:
fp.write(data_Str)
fp.write('\n'+ labels_Str)
with open("Exporting_data.txt", "rt") as fin:
with open("Exporting_data_new.txt", "wt") as fout:
for line in fin:
fout.write(line.replace('preds', '').lstrip())
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.