Python資料清洗 從檔案讀取資料行並去重

2021-09-19 10:29:23 字數 647 閱讀 8365

假設現在log.txt中有若干行資料:

zhang

phil

zhang phil

zhang

phil

zhang phil

可以看出log.txt檔案中存在多處重複的資料,現在做一些資料清洗工作把重複的資料去重只保留乙份,然後寫入新的檔案newfile.txt中。

def read_file(filename):

f = open(filename, "r")

ss = list(f)

new_ss =

for s in ss:

if s not in new_ss:

f.close()

f = open("newfile.txt", "w")

for line in new_ss:

f.write(line)

f.close()

if __name__ == "__main__":

read_file("log.txt")

此時新的檔案newfile.txt中內容如下:

zhang

phil

zhang phil

Python資料清洗之 刪除讀取失敗檔案

import os import shutil import warnings from pil import image warnings.filterwarnings error category userwarning def is read successfully file try ima...

Python 從檔案中讀取資料

學習python時,發現在使用with open pi digits.text as file object時,使用相對路徑,總是出現notfoundfileerror的錯誤,後來想到使用絕對路徑。書中提到的在linux環境中路徑使用的是斜槓 在windows環境中使用的是反斜槓 而經過實踐,發現只...

Python從檔案中讀取資料

編寫了乙個名為learning python.txt的檔案,內容如下 root centos7 tmp cat learning python.txt in python you can code in python you can learn object in python you can le...