[英]Compare each element in CSV with other using python
I want to compare each element in a csv file with all other elements using python. 我想使用python将csv文件中的每个元素与所有其他元素进行比较。 I have made 2 columns which are exacly same thinking I can iterate over each row.col pair. 我已经做了两列,它们完全相同,我可以遍历每个row.col对。 File looks like this 文件看起来像这样
NAME NAME_COMPARE AAA AAA BBB BBB NAME NAME_COMPARE AAA AAA BBB BBB
The output I would like to see is: AAA,AAA AAA,BBB BBB,AAA BBB,BBB 我想看到的输出是:AAA,AAA AAA,BBB BBB,AAA BBB,BBB
here is the code I am using 这是我正在使用的代码
fname = 'UA_TEST.csv'
fp = open(fname)
fp.next()
cscrd = (csv.reader(fp, delimiter='\t', doublequote=True))
for row in cscrd:
a = row[1]
for row in cscrd:
b = row[2]
print a,b
Code gives following output 代码给出以下输出
AAA,AAA AAA,BBB AAA,AAA AAA,BBB
and then it exits it never goes through the second loop. 然后退出,再也不会经过第二个循环。
Any pointers? 有指针吗?
I think you need something like this, 我想你需要这样的东西
import csv
fname = 'UA_TEST.csv'
fp = open(fname)
fp.next()
cscrd = (csv.reader(fp, delimiter='\t', doublequote=True))
i = 0
for row in cscrd:
a = row[i]
for col in row:
b = col
print a,b
i += 1
This gives the output: 这给出了输出:
AAA AAA
AAA BBB
BBB AAA
BBB BBB
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.