python writerow(head)重复表头_Python-在csv文件中显示具有重复值的行

我有一个.csv文件,有几列,其中一列是随机数,我想在那里找到重复的值。如果有—奇怪的情况,但这毕竟是我要检查的—我想显示/存储存储存储这些值的完整行。

要说清楚,我有这样的东西:First, Whatever, 230, Whichever, etc

Second, Whatever, 11, Whichever, etc

Third, Whatever, 46, Whichever, etc

Fourth, Whatever, 18, Whichever, etc

Fifth, Whatever, 14, Whichever, etc

Sixth, Whatever, 48, Whichever, etc

Seventh, Whatever, 91, Whichever, etc

Eighth, Whatever, 18, Whichever, etc

Ninth, Whatever, 67, Whichever, etc

我想要:Fourth, Whatever, 18, Whichever, etc

Eighth, Whatever, 18, Whichever, etc

为了找到重复的值,我将该列存储到字典中,并对每个键进行计数,以发现它们出现的次数。import csv

from collections import Counter, defaultdict, OrderedDict

with open(file, 'rt') as inputfile:

data = csv.reader(inputfile)

seen = defaultdict(set)

counts = Counter(row[col_2] for row in data)

print "Numbers and times they appear: %s" % counts

我明白了Counter({' 18 ': 2, ' 46 ': 1, ' 67 ': 1, ' 48 ': 1,...})

现在问题来了,因为我没办法把这个键和重复项联系起来,然后再计算。如果我做了for value in counts:

if counts > 1:

print counts

我只拿钥匙,这不是我想要的,也不是所有的价值(更不用说我想打印的不仅仅是这个,还有整行…)

基本上我在找一种方法If there's a repeated number:

print rows containing those number

else

print "No repetitions"

提前谢谢。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值