Python 刪除文件每一行的行號(hào)思路解讀
發(fā)布日期:2021-12-13 22:17 | 文章來(lái)源:源碼中國(guó)
1. what

這個(gè)行號(hào)真的很煩噶 試著寫一個(gè)py去掉
2. 思路
def second_of_str分割,取分隔符右邊的元素返回一個(gè)列表def move傳入文件路徑,讀取每行,列表存儲(chǔ),調(diào)用 second_of_str分割后的存入新列表- 主函數(shù)調(diào)用move
3. 代碼
def second_of_str(str,splitsymbol):
s = str.split(splitsymbol,1)# 分隔符右邊的元素
# if len(s) == 1:
# return []
return s[1]
def move(file_path, new_file_path):
with open(file_path,encoding='utf-8') as f:
content = f.read().splitlines()
# for line in content:
# print(line)
new_content = []
for line in content:
new_line = second_of_str(line,'.')
# 去掉后的每行加入新列表
new_content.append(new_line)
for line in new_content:
print(line)
f1 = open(new_file_path,encoding='utf-8',mode = 'w')
for line in new_content:
f1.writelines(line)
f1.writelines(["\n"])
f1.close()
return new_content
if __name__ == '__main__':
f = move('1.txt','2.txt')
完成啦,成果~

到此這篇關(guān)于Python 刪除文件每一行的行號(hào)思路解讀的文章就介紹到這了,更多相關(guān)Python 刪除文件行號(hào)內(nèi)容請(qǐng)搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來(lái)源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來(lái)源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來(lái)源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來(lái),僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。
相關(guān)文章
關(guān)注官方微信