1

I have this piece of code:

with open(filepath, 'rb') as csvfile:
    reader = csv.reader(csvfile, delimiter=' ')
    for row in reader:
        print(' '.join(row))

and it returns:

Type,UniProt ID,Position
Sodium channel,P35498,1-123
Sodium channel,P35498,176-188
Sodium channel,P35498,234-239
Sodium channel,P35498,426-762
Sodium channel,P35498,823-830
Potassium channel ATP-sensitive inward rectifier,P48048,1-77

I would like to be able to put P35498 in a string to use later. How would I do this? I would also like to be able to get any of those columns but just an example with P35498 would be great, thanks!

If I do

with open(filepath, 'rb') as csvfile:
    reader = csv.reader(csvfile, delimiter=',')
    idlist = []
    for row in reader:
        idlist.append(row[1])
        print(idlist)

This is returned:

['UniProt ID']
['UniProt ID', 'P35498']
['UniProt ID', 'P35498', 'P35498']
['UniProt ID', 'P35498', 'P35498', 'P35498']
['UniProt ID', 'P35498', 'P35498', 'P35498', 'P35498']
['UniProt ID', 'P35498', 'P35498', 'P35498', 'P35498', 'P35498']
['UniProt ID', 'P35498', 'P35498', 'P35498', 'P35498', 'P35498', 'P48048']
3
  • So you want to get only the row which has p35498 Commented Jul 16, 2015 at 16:06
  • Yes, but I would still like to print the entire row. @VigneshKalai Commented Jul 16, 2015 at 16:07
  • what is your expected output Commented Jul 16, 2015 at 16:16

3 Answers 3

1

If you want the row which has "P35498" in column 2 you could check column 2 and print so when it is same :

with open(filepath, 'rb') as csvfile:
    reader = csv.reader(csvfile, delimiter=' ')
    for row in reader:
        if "P35498"==row[1]:
            print(' '.join(row))
Sign up to request clarification or add additional context in comments.

Comments

1

row is a list, you can use indexes to get the data for the corresponding column. For example for column ID , which is the second column , so index 1 , you would use -

with open(filepath, 'rb') as csvfile:
    reader = csv.reader(csvfile, delimiter=',')
    idlist = []
    for row in reader:
        idlist.append(row[1])
    print(idlist)

At the end, idlist would be a list of all ids in the csv.

Also, according to your example , you should use , as the delimiter.

As said in the question comments, If the ask is to print a row with a specific id you can use -

with open(filepath, 'rb') as csvfile:
    reader = csv.reader(csvfile, delimiter=',')
    for row in reader:
        if 'P35498' in row[1]:
            print(' '.join(row))

7 Comments

I think the actual ID is UniProt ID, which is column 2, and would be index 1, so row[1]
Yes thanks, the delimiter according to example should be , not space.
@AnandSKumar correct me if I am wrong but he said all row right
@VigneshKalai Not sure i thought the ask was to get the column , but anyway added that to the answer as well. Thanks.
@AnandSKumar if you do your first answer. Look at my edit.
|
0

Actually, just doing

    with open(filepath, 'rb') as csvfile:
    reader = csv.reader(csvfile, delimiter=',')
    for row in reader:
        uniprot_id = row[1]
        print uniprot_id

works.

2 Comments

That code is exactly same as - unitprot_id = row[1]; print uniprot_id , why you using the soooo big route?
@AnandSKumar I was going to create a list with all of the ids but I decided against it and just didn't edit it...and I put uniprot_id_list = [] in the wrong place... thanks for reminding me, I would've actually kept it there!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.