我想比較 2 個 csv 檔案 master.csv 和 exclude.csv 并洗掉基于 column1 的所有匹配行并將最終輸出寫入 mater.csv 檔案。
大師.csv
abc,xyz
cde,fgh
ijk,lmn
排除.csv
###Exclude list###
cde
####
預期輸出(它應該覆寫 master.csv
abc,xyz
ijk,lmn
試到現在
with open('exclude.csv','r') as in_file, open('master.csv','w') as out_file:
seen = set()
for line in in_file:
if line in seen: continue # skip duplicate
seen.add(line)
out_file.write(line)
uj5u.com熱心網友回復:
我相信應該有一些pandas或其他模塊方法,但這是一個純粹的 pythonic 方法:
with open("master.csv") as f:
master = f.read()
with open("exclude.csv") as f:
exclude = f.read()
master = master.strip().split("\n")
exclude = exclude.strip().split("\n")
returnList = []
for line in master:
check = True
for exc in exclude:
if exc in line:
check = False
break
if check:
returnList.append(line)
with open("master.csv", "w") as f:
f.write("\n".join(returnList))
master.csv 的輸出
abc,xyz
ijk,lmn
uj5u.com熱心網友回復:
使用熊貓的最簡單方法..
import pandas as pd
# Reading the csv file
df_new = pd.read_csv('Names.csv')
# saving xlsx file
GFG = pd.ExcelWriter('Names.xlsx')
df_new.to_excel(GFG, index=False)
GFG.save()
uj5u.com熱心網友回復:
利用串列推導的純 Pythonic 答案:
with open('master.csv', 'r') as f:
keep_lines = f.readlines()
with open('exclude.csv', 'r') as f:
drop_lines = f.readlines()
write_lines = [line[0] for line in zip(keep_lines, drop_lines) if line[0].strip().split(',')[0] != line[1].strip()]
with open('master.csv', 'w') as f:
f.writelines(write_lines)
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/461229.html
標籤:Python python-3.x 熊猫 python-2.7
下一篇:劊子手游戲中的提示
