1

I was try to learn how to find a string or value in csv using python. i was think it's simple but when i was try to execute my code, i don't understand why my code cannot find the string what i want. This is my code :

import csv

with open('sample2.csv', 'rt') as baca:
    read = csv.reader(baca, delimiter='\'')
    for row in read:
        match = row[0]
        if match == "Sample":
            print match

And also i was try to print "row[0]" and the result is like this :

id      name         number
1       Sample    0123450000007 

Print a "row" without index :

{'id\tname\tnumber': '1\tSample\t0123450000007'}

The screenshot :

enter image description here

4
  • 2
    I guess you need match = row[1] Commented Aug 11, 2017 at 2:57
  • I was try that, but i got error. "list index out of ranger" Commented Aug 11, 2017 at 3:13
  • 1
    how does your data looks. May be put some sample lines Commented Aug 11, 2017 at 3:15
  • I update my question Commented Aug 11, 2017 at 3:22

2 Answers 2

2

It looks like demilter should be a tab

In [9]: import csv
   ...:
   ...: with open('sample2.csv', 'rt') as baca:
   ...:     read = csv.reader(baca, delimiter='\t')
   ...:     for row in read:
   ...:         match = row[1]
   ...:         if match == "Sample":
   ...:             print match
Sample
Sign up to request clarification or add additional context in comments.

4 Comments

it's working!! How you do that ? I mean, i was using row[1] but i got the error "list index out of range", can you explaint to me ??
the delimiter you had was incorrect so the record was not split into seperate fields and the print row shows that delimiter is \t. In my answer I gave \t, which means TAB as delimiter.
now,i understand. Thank you so much for your help, @amit.
@Scarlettstone - glad to help !
1

You could also use csv.DictReader which makes it easier to select the item you want. Assuming your CSV looks like this:

id,name,number
1,Sample,234234
2,NotAMatch,567657

You can print the matching rows like this:

In [159]: import csv
In [160]: with open('samples.csv') as f:
     ...:     reader = csv.DictReader(f, delimiter='\t')
     ...:     for row in reader:
     ...:         if row['name'] == "Sample":
     ...:             print(row)
     ...:             
OrderedDict([('id', '1'), ('name', 'Sample'), ('number', '234234')])

Or a little shorter with a list comprehension:

In [163]: with open('samples.csv') as f:
     ...:     reader = csv.DictReader(f)
     ...:     print([row for row in reader if row['name'] == "Sample"])

4 Comments

it's still give me error. the error is "Key error : 'name' "
Post a sample of your CSV including any header row.
@Scarlettstone look at my updated answer. You just need to change your delimiter to '\t'.
It would be best to post it in text format. LibreOffice doesn't show what the raw formatting of the file looks like. But I'm pretty sure changing the delimiter will solve your problem.

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.