1. what
這個行號真的很煩噶 試著寫一個py去掉
2. 思路
-
def second_of_str
分割,取分隔符右邊的元素返回一個列表 -
def move
傳入文件路徑,讀取每行,列表存儲,調用 second_of_str分割后的存入新列表 - 主函數調用move
3. 代碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
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' ) |
完成啦,成果~
到此這篇關于Python 刪除文件每一行的行號思路解讀的文章就介紹到這了,更多相關Python 刪除文件行號內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://blog.csdn.net/grb819/article/details/121149513