python重复读csv文件_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"

提前致谢.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值