用Python读取CSV

时间:2015-02-26 01:56:07

标签: python

我坚持一个问题。我试图找出如何使用PYTHON编写程序来检查所有记录是否相同。到目前为止,我想出了下面的代码。如何检查所有记录而不仅仅是行内的数据相等?

import csv

index = 0

def all_same(items):
    return all(x == items[0] for x in items)


with open(r'C:\Users\Aaron\Desktop\testfolder\data.csv') as csvfile:
    readCSV = csv.reader(csvfile, delimiter = ',')

    for row in readCSV:
            print (row)
            print (all_same(row))

输出如下:

['1', '1', '1', '2']
False
['1', '1', '1', '1']
True
['1', '1', '1', '1']
True

4 个答案:

答案 0 :(得分:1)

如果您愿意,可以将它们全部放入列表中,然后检查该列表的值:

data = []
for row in readCSV:
    data.append(row)

print (all_same(data))

答案 1 :(得分:1)

你可以试试这个:

import csv

with open(r'C:\Users\Aaron\Desktop\testfolder\data.csv') as csvfile:
  readCSV = csv.reader(csvfile, delimiter = ',')
  firstrow = readCSV.next()
  print(all(row==firstrow for row in readCSV))

答案 2 :(得分:1)

您可能希望在下面列出的Pandas数据框中的匹配行中看到类似的问题。该问题中的一个解决方案也适用于此。

import pandas as pd

csv_filename = "file.csv" #your filepath
raw_data = pd.read_csv(csv_filename)

tmp = raw_data    
for idx, val in random_sample.iteritems():
    try:
        if np.isnan(val):
            continue
    except:
        pass
    tmp = tmp[tmp[idx] == val]
if len(tmp) == 1: print "match"

Efficiently find matching rows (based on content) in a pandas DataFrame

答案 3 :(得分:1)

不要忘记熊猫

import pandas
df = pandas.read_csv(r'C:\Users\Aaron\Desktop\testfolder\data.csv')
pandas.unique(df.values)