0

Suppose I have 2 CSV files:

file 1:

Epitope Name,Epitope,Protein,position,position

3606,NSRSTSLSV,FOO,10,21

File 2:

A,B,C,D,E,F,G,H,I,J,K

0,1,2,3,4,5,6,7,8,9,NSRSTSLSV

Essentially, I want to see if the contents of row 1 in file 1 are found in row 10 of file 2. If the contents match, I'll print a 3rd csv that is a new version of file 1 with a column saying found or not found.

Right now, I'm getting not found for everything, which I know not to be the case. In some cases, the text from file 1 may be found inside a larger block of text from file 2.

Here's what I have so far (adapted from an answer found earlier):

#usr/bin/python2.4

import csv

f1 = file ('all_epitopes.csv', 'rb')
f2 = file ('positiveBcell.csv', 'rb')
f3 = file ('results.csv', 'w')

c1 = csv.reader((f1), delimiter=",", quotechar='"')
c2 = csv.reader((f2), delimiter=",", quotechar='"')
c3 = csv.writer((f3), delimiter=",", quotechar='"')


positiveBcell = [row for row in c2]

for all_epitopes_row in c1:
    row = 1
    found = False
    for master_row in positiveBcell:
        results_row = all_epitopes_row
        if all_epitopes_row[2] == positiveBcell[10]:
            results_row.append('FOUND in Bcell List (row ' + str(row) + ')')
            found = True
            break
        row = row +1
    if not found:
        results_row.append('NOT FOUND in Bcell list')
    c3.writerow(results_row)

f1.close()
f2.close()
f3.close()
4

1 回答 1

0

假设你的两个文件

文件 1:

Epitope Name,Epitope,Protein,position,position

#Row 1#
3606,NSRSTSLSV,FOO,10,21

文件 2:

A,B,C,D,E,F,G,H,I,J,K

#Row 10#
0,1,2,3,4,5,6,7,8,9,NSRSTSLSV

在OP发表评论后:

for all_epitopes_row in c1:
    row = 1
    found = False
    for master_row in positiveBcell:
        results_row = all_epitopes_row
        **if all_epitopes_row[2] == master_row[10]:**
            results_row.append('FOUND in Bcell List (row ' + str(row) + ')')
            found = True
            break
        row = row +1
    if not found:
        results_row.append('NOT FOUND in Bcell list')
    c3.writerow(results_row)
于 2013-07-29T19:29:12.047 回答