Python刪除檔案中包含某關鍵字的一整行

2021-09-27 12:01:28 字數 709 閱讀 6461

拿到手的txt檔案中大概有10萬多條資料,分為8個檔案

由於每個檔案中某些字段包含空值,因此使用python對該資料進行資料清洗。

**片段:

// 使用python進行資料清洗

import re

list =

matchpattern = re.

compile

("null"

)f_path=r'所要處理的txt檔案路徑,(注意檔案路徑應為d:\\....)'

with

open

(f_path)

as f:

while1:

line = f.

readline()

if not line:

break

elif matchpattern.

search

(line)

: pass

else

: list.

(line)

f.close()

f =open

('所要寫入的txt檔案路徑,(注意檔案路徑應為d:\\....)'

,'w'

)for i in list:

f.write

(i) f.

close

()

python 刪除list中某所有元素

突然記起來一年前剛入行時,領導面試我時提的問題,刪除list中所有等於某個值的元素 比如 刪除 1,2,3,4,1,2,3,4,1,3,4 中所有的2元素 刪除元素常用 remove,pop 方法,remove可以直接刪除某個值 list.remove 1 但是每次只能刪除第乙個,且如果list中不...

Python刪除檔名包含指定字元的檔案

思路 1,先讀取要操作的資料夾 2,算了直接看 有嘆號那裡 coding utf 8 import os deftest05 filepath c 3 name os.listdir filepath 列印所指定資料夾內的所包含 的檔案數 檔名 print len name print name f...

Python 實現刪除某路徑下檔案及資料夾

python 實現刪除某路徑下檔案及資料夾的指令碼 usr bin env python import os import shutil dellist deldir home test dellist os.listdir deldir for f in dellist filepath os.p...