日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java删除csv一行_如何删除两个CSV之间的不常见行?

發布時間:2025/3/15 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java删除csv一行_如何删除两个CSV之间的不常见行? 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

創建演示數據:

# bad data, the weird ones are bad

data = """

ts1,d001,d002,d003

ts2,d001,d002,d003

ts3,d001,d002,d003

weird1,d001,d002,d003

weird2,d001,d002,d003

ts4,d001,d002,d003

"""

# the good data

other = """

ts1,f001,f002,f003

ts2,f001,f002,f003

ts3,f001,f002,f003

ts4,f001,f002,f003

"""

# create demo files

fn1 = "d1.csv"

fn2 = "d2.csv"

with open(fn1,"w") as f:

f.write(data)

with open(fn2,"w") as f:

f.write(other)

現在解析:

import csv

def readFile(name):

"""returns a dict for data with 4 columns"""

result = []

with open(name,"r") as f:

k = csv.DictReader(f,fieldnames=["ts","dp1","dp2","dp3"])

for l in k:

result.append(l)

return result

badData = readFile(fn1)

goodData = readFile(fn2)

print(badData)

print(goodData)

輸出:

# weired data

[{'dp3': 'd003', 'ts': 'ts1', 'dp1': 'd001', 'dp2': 'd002'},

{'dp3': 'd003', 'ts': 'ts2', 'dp1': 'd001', 'dp2': 'd002'},

{'dp3': 'd003', 'ts': 'ts3', 'dp1': 'd001', 'dp2': 'd002'},

{'dp3': 'd003', 'ts': 'weird1', 'dp1': 'd001', 'dp2': 'd002'},

{'dp3': 'd003', 'ts': 'weird2', 'dp1': 'd001', 'dp2': 'd002'},

{'dp3': 'd003 ', 'ts': 'ts4', 'dp1': 'd001', 'dp2': 'd002'}]

# good data

[{'dp3': 'f003', 'ts': 'ts1', 'dp1': 'f001', 'dp2': 'f002'},

{'dp3': 'f003', 'ts': 'ts2', 'dp1': 'f001', 'dp2': 'f002'},

{'dp3': 'f003', 'ts': 'ts3', 'dp1': 'f001', 'dp2': 'f002'},

{'dp3': 'f003 ', 'ts': 'ts4', 'dp1': 'f001', 'dp2': 'f002'}]

現在要消除不良數據點:

# get all the "good" ts

goodTs = set( oneDict["ts"] for oneDict in goodData)

# clean the bad data, only keep those "ts" that are in goodTs

cleanedData = [x for x in badData if x["ts"] in goodTs]

print(cleanedData)

輸出:

# filtered weired data

[{'dp3': 'd003', 'ts': 'ts1', 'dp1': 'd001', 'dp2': 'd002'},

{'dp3': 'd003', 'ts': 'ts2', 'dp1': 'd001', 'dp2': 'd002'},

{'dp3': 'd003', 'ts': 'ts3', 'dp1': 'd001', 'dp2': 'd002'},

{'dp3': 'd003 ', 'ts': 'ts4', 'dp1': 'd001', 'dp2': 'd002'}]

完成 .

總結

以上是生活随笔為你收集整理的java删除csv一行_如何删除两个CSV之间的不常见行?的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。