0

so I basically finished the code but the problem is I can't access the first line of the csv file. What I did was this

with open(sys.argv[1], newline='') as database:
reader = csv.reader(database)
col = len(next(reader))  # get number of columns 
reader = list(reader) 
row = len(reader)

So whenever I want to access a specific block, I do reader[0][0]. The problem is reader[0][0] comes out as the name of the first person. For example, I wanted to access the "AGATC" which logically I would do reader0 but it would access 15 instead. the [0] is the second line. I want it to access that first line so that I can know what are the boundaries of the program(like is the database the large one or the small one) or even if the person uses a different csv file with different STR's. Without the access, I had to do is make count the columns of the csv file. This is very inefficient. I would like to know is there a way to fix this or is there anything else I can do. I am new to python so it's a bit hard for me. Thank you in advance!

if col == 9:
    array = [agatc, ttttttct, aatg, tctag, gata, tatc, gaaa, tctg]
else:
    array = [agatc, aatg, tatc]

enter image description here

2

According to the python csv doc, this col = len(next(reader)) # get number of columns "Return[s] the next row of the reader’s iterable object as a list", which in this case is the first row. This reader = list(reader) returns the "rest" of the rows in the iterable object. If you get rid of the first line (the col =), that next line will convert the iterable object into a list, and reader[0] will be the first line.

| improve this answer | |
0

What I did was col = len(reader[0]) to get the number of columns in the list.

| improve this answer | |

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .