![](/img/trans.png)
[英]How can I trim the string before first white space in a series Python text data?
[英]How do i remove the very first white space from my text file in Python?
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()
我這樣做是為了刪除我的文本文件(preds)的第一個單詞,但現在它留下了一個我不知道如何刪除的空白。 文本文件現在看起來像這樣。
B-Product B-Product B-Product B-Product
從最初,
preds B-Product B-Product B-Product B-Product
您可以使用lstrip
去除前導空格:
fout.write(line.replace('preds', '').lstrip())
使用.lstrip()
+修復+壓痕使用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())
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.