我想更改文本檔案中每一行的第一個字符。這是我的代碼和輸出,但我不知道如何更改并將其保存在同一個文本檔案中。
for rf in glob.glob('./labels/train/*'):
read=open(rf,'r')
t=read.readlines()
for ind in t:
print(ind[0])
我得到了我想要的,但我不知道如何更改它并將其保存在同一個文本檔案中。
1
1
1
1
1
4
uj5u.com熱心網友回復:
試試這個:)
for rf in glob.glob('./labels/train/*'):
read=open(rf,'r')
t=read.readlines()
read.close()
for_write = [str(int(ind[0])-1) ind[1:] for ind in t]
write = open(rf,'w')
write.writelines(for_write)
write.close()
或使用with:)
for rf in glob.glob('./labels/train/*'):
with open('test.txt','r')as read:
t=read.readlines()
for_write = [str(int(ind[0])-1) ind[1:] for ind in t]
with open('test.txt','w') as write:
write.writelines(for_write)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/470243.html
上一篇:如何在熊貓子集中參考自我
下一篇:如何進行數學運算熊貓資料框
