Python 如何刪除檔案中的空白行?

2022-04-11 04:02:30 字數 756 閱讀 7799

defdelblankline(infile,outfile):

infopen = open(infile,'r'

) outfopen = open(outfile,'w'

) lines =infopen.readlines()

for line in

lines:

ifline.split():

outfopen.writelines(line)

else

: outfopen.writelines("")

infopen.close()

outfopen.close()

delblankline(

"output.py

","output1.py

")

1. python split()通過指定分隔符對字串進行切片,返回分割後的字串列表。str.split()分隔符預設為空格。

2. 函式 writelines(list)

函式writelines可以將list寫入到檔案中,但是不會在list每個元素後加換行符,所以如果想每行都有換行符的話需要自己再加上。

例如:for line in lines:

outfopen.writelines(line+"\n")

3. .readlines() 自動將檔案內容分析成乙個行的列表,該列表可以由 python 的 for ... in ... 結構進行處理。

python如何刪除列為空的行

1.摘要 dropna 方法,能夠找到dataframe型別資料的空值 缺失值 將空值所在的行 列刪除後,將新的dataframe作為返回值返回。2.函式詳解 函式形式 dropna axis 0,how any thresh none,subset none,inplace false 引數 ax...

Python 刪除列表中的空元素, n t元素

本以為挺簡單的,一頓操作之後,再加上網上的資料,還有點小複雜。以案例來說,更清晰些,做個學習筆記。list eg hello n world t print list eg 輸出如下 hello n world t list eg change x.strip for x in list eg if...

如何刪除ini檔案中的內容

1 刪除子項值 writeprivateprofilestring 分割槽名稱,子項名稱,ini檔案路徑 2 刪除子項 名稱和值 writeprivateprofilestring 分割槽名稱,子項名稱,null,ini檔案路徑 3 刪除分割槽下所有子項 writeprivateprofilesec...