#๐Ÿ”’ ๐Ÿ”’ csv reader

38 messages ยท Page 1 of 1 (latest)

misty compass
#
filename = 'SJOB11251_diann_search_DIANN_report.stats_3.0_fix.csv'
with open(filename) as f:
    reader = csv.reader(f)
    header_row = next(reader)

    # Get filenames and peptides/proteins from this file.
    filenames, proteins, peptides = [], [], []
    for row in reader:
        filename = str(row[0][0:8])
        peptide = int(row[1])
        protein = int(row[2])
        filenames.append(filename)
        peptides.append(peptide)
        proteins.append(protein)

why do i need the "header_row = next(reader)" line? it makes my code fail if i remove it even though i dont explicitly reference the "header_row" variable again

warm doveBOT
#

@misty compass

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

restive venture
misty compass
#

!close

warm doveBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.

#

๐Ÿ”’ csv reader

restive venture
misty compass
#

no

#

i thought that would get rid of the help text stuff

#

sorry

restive venture
misty compass
#

yeah from @warm dove never mind though i didnt mean to close it

#

and no im not sure i follow what ur saying

#

i have my csv in front of me though

restive venture
misty compass
#

yeah

restive venture
#

so doing next(reader) gives you the next line of reader. which in this case is the first line.

misty compass
#

is header_row a variable im making up or is it inherent to the csv reader

#

so it's pointing the reader variable to another line ?

#

why am i using a variable to do that

restive venture
misty compass
#

why do i need to do that with "header_row"

restive venture
#

so that when you do for row in reader:, reader continues from the second line, where the actual data starts.

misty compass
#

is there a way to do that without using a variable

restive venture
# misty compass is there a way to do that without using a variable

I don't know exactly what you mean by "using a variable", but it's not required that you assign the result of next(reader) to a variable, if you're sure you don't want to use it.

with open(filename) as f:
    reader = csv.reader(f)
    next(reader)  # no variable
    # continue as before
misty compass
#

let me try that

#

ok it works the same

#

thanks

#

im not sure why it was written like that but you can make a permanent action still happen even if it's in a variable assignment ?

restive venture
#

next(reader) returns a string of whatever the next line is

#
print(next(reader).upper())

you could do this if you wanted to print the next line in all caps. idk why you would.

misty compass
#

ok

#

i'll close thanks

restive venture
#

bye bye

misty compass
#

!close

warm doveBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.