How do I find the row number for a specific item in a CSV file in Python?

17,335

Solution 1

It appears that the csv.reader gives you each row as a list, so you need to index into that list to get the string for comparison.

If you change your code to the following, you will get the zero-based index.

import csv

def find_index(input): 
    o = open('products.csv', 'r') 
    myData = csv.reader(o) 
    index = 0 
    for row in myData:
      #print row
      if row[0] == input: 
        return index 
      else : index+=1
print find_index('d')

If you uncomment the print statement, the problem will become obvious.

Solution 2

You're looking for the line_num member of the reader object:

with open('products.csv') as o:
  myData = csv.reader(o):
  for row in myData:
    if row == input:
       return myData.line_num

Do be aware that this is not the same as the number of records returned, as records can span multiple lines. But, generally, if your record terminator is the newline, they will be identical. If you need further assistance, leave a comment.

Share:
17,335
addybist
Author by

addybist

software dev

Updated on June 14, 2022

Comments

  • addybist
    addybist almost 2 years

    I have been looking for a solution but I haven't found one yet. So, I'm trying to find the row number of a specific input in my csv file. For example, let this be the csv file I'm parsing -

    a
    b
    c
    d
    

    This is a file with 100 rows and 1 column. So I give in the input "d" and I should get 4. I tried this code but it gives me None -

    def find_index(input):
        o = open('products.csv', 'rU')
        myData = csv.reader(o)
        index = 0
        for row in myData:
            if row == input:
                return index
            else : index+=1
    

    I'm new with the csv module, so please forgive me if I'm being stupid. Thanks!

    EDIT - I tried everyone's solution and all of them just return None. What could be wrong? Is it the 'rU' flag? Because if I remove that, I get this error - for row in myData:

    Error: new-line character seen in unquoted field - do you need to open the file in universal-newline mode?
    
  • Jon Clements
    Jon Clements about 10 years
    Since they're asking for row number, I would be tempted to suggest for rowno, row in enumerate(myData, 1) then return rowno instead... But very good point about .line_num
  • addybist
    addybist about 10 years
    Interestingly, printing row actually gives me what I want
  • John Y
    John Y about 10 years
    Given what the OP is describing, this is the answer. It has everything to do with row not being equal to input and nothing to do with "what's a more concise or idiomatic way to get the row number".
  • John Y
    John Y about 10 years
    Honestly, whoever upvoted this is just robo-voting. This doesn't answer the question at all. The information provided isn't bad, but it doesn't help the OP overcome the stated problem in the slightest.
  • addybist
    addybist about 10 years
    This, too gives me None as an answer though
  • merlin2011
    merlin2011 about 10 years
    @AdityaBist, please upload your data file somewhere. I think there might be a formatting issue with your file. Also, which version of Python are you on?
  • addybist
    addybist about 10 years
    @merlin2011, I'm using Python 2.7.3. I'm basically copy pasting your code but it doesn't give me anything.
  • merlin2011
    merlin2011 about 10 years
    @AdityaBist, I am also using 2.7.3. If I copy your example into a file called products.csv and run the code above, it gives 3. I am edited my answer to add the function call.
  • John Y
    John Y about 10 years
    @AdityaBist: You're going to get better help (and better results for yourself!) if you try to actually understand what people are telling you. Please forgive my bluntness, but right now it seems like you're not thinking for yourself, and just copying whatever code you see on the screen. The key thing merlin2011 is saying, which I tried to highlight in my comment, is that row is never equal to input. Why is that? Well, for one, row is always a list and input never is. So for sure that can't work. What does repr(row) look like? What does repr(input) look like? Look for clues.
  • addybist
    addybist about 10 years
    @JohnY I did try to understand, and that's why I wrote I'm new and possibly asking stupid questions. The only reason I wrote I'm copy pasting the code is because merlin2011 asked me to upload the file of my code, and I wanted to assure him I'm following his instructions.
  • addybist
    addybist about 10 years
    @merlin2011 Your example works in mine too. I gave the example a,b,c,d because I didn't want to give my actual work. Looks like if it's a long word, it works differently from a character. Let's say the example was apples are great bananas are good oranges are okay This won't work.
  • merlin2011
    merlin2011 about 10 years
    @AdityaBist, I am assuming those are on separate lines.
  • merlin2011
    merlin2011 about 10 years
    @AdityaBist, I tried your new example and my code still works. The case where it does NOT work is if you have a trailing space in your file, and you are trying to search for the string without the trailing space. If you have a trailing or leading space, then you should change row[0] to row[0].strip(), and that should handle that.
  • John Y
    John Y about 10 years
    @AdityaBist: I get that you are new, but the reason it seemed like you weren't really trying to think for yourself is that the trailing space thing should have been revealed right at the beginning by print row. Your first comment on this answer (that printing row actually gives you what you want) doesn't make any sense at all. Then you didn't seem to take my advice to check repr(row) or repr(input). You waited until merlin2011 happened to guess and figure out the problem for you.
  • addybist
    addybist about 10 years
    When I printed the row, it gave me the sentence I was looking for. This was surprising, as I stated, because merlion said it's a list. So I was expecting only a word to be there.