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
答案 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)