How do I find the row number for a specific item in a CSV file in Python?
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.
Comments
-
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 about 10 yearsSince they're asking for row number, I would be tempted to suggest
for rowno, row in enumerate(myData, 1)
then returnrowno
instead... But very good point about.line_num
-
addybist about 10 yearsInterestingly, printing row actually gives me what I want
-
John Y about 10 yearsGiven what the OP is describing, this is the answer. It has everything to do with
row
not being equal toinput
and nothing to do with "what's a more concise or idiomatic way to get the row number". -
John Y about 10 yearsHonestly, 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 about 10 yearsThis, too gives me None as an answer though
-
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 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 about 10 years@AdityaBist, I am also using
2.7.3
. If I copy your example into a file calledproducts.csv
and run the code above, it gives3
. I am edited my answer to add the function call. -
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 toinput
. Why is that? Well, for one,row
is always a list andinput
never is. So for sure that can't work. What doesrepr(row)
look like? What doesrepr(input)
look like? Look for clues. -
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 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 about 10 years@AdityaBist, I am assuming those are on separate lines.
-
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]
torow[0].strip()
, and that should handle that. -
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 printingrow
actually gives you what you want) doesn't make any sense at all. Then you didn't seem to take my advice to checkrepr(row)
orrepr(input)
. You waited until merlin2011 happened to guess and figure out the problem for you. -
addybist about 10 yearsWhen 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.