2

I want to compare each element in a csv file with all other elements using python. I have made 2 columns which are exacly same thinking I can iterate over each row.col pair. File looks like this

NAME NAME_COMPARE AAA AAA BBB BBB

The output I would like to see is: 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

and then it exits it never goes through the second loop.

Any pointers?

4

1 回答 1

1

我想你需要这样的东西,

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

这给出了输出:

AAA AAA
AAA BBB
BBB AAA
BBB BBB
于 2013-08-06T23:33:02.197 回答