33

I need a way to get a specific item(field) of a CSV. Say I have a CSV with 100 rows and 2 columns (comma seperated). First column emails, second column passwords. For example I want to get the password of the email in row 38. So I need only the item from 2nd column row 38...

Say I have a csv file:

aaaaa@aaa.com,bbbbb
ccccc@ccc.com,ddddd

How can I get only 'ddddd' for example?

I'm new to the language and tried some stuff with the csv module, but I don't get it...

NightShadeQueen
  • 3,284
  • 3
  • 24
  • 37
Joko
  • 351
  • 1
  • 3
  • 6
  • @ThiefMaster: Is your edit correct? First it seemed the OP wanted the last term of a line... Otherwise the question is difficult to understand: Do the OP want the last string after the last comma in the csv file? – joaquin Apr 22 '11 at 16:54
  • @joaquin: All I changed was replacing `???` with `?`. And John's edit just added code formatting (the question's code already had two lines before) so I'm pretty sure it is. – ThiefMaster Apr 22 '11 at 17:17
  • 1
    The file is email,password per row for example 1st column email, second column password. I want to get only the password of the 2nd row for example – Joko Apr 22 '11 at 17:40

7 Answers7

33
import csv
mycsv = csv.reader(open(myfilepath))
for row in mycsv:
   text = row[1]

Following the comments to the SO question here, a best, more robust code would be:

import csv
with open(myfilepath, 'rb') as f:
    mycsv = csv.reader(f)
    for row in mycsv:
        text = row[1]
        ............

Update: If what the OP actually wants is the last string in the last row of the csv file, there are several aproaches that not necesarily needs csv. For example,

fulltxt = open(mifilepath, 'rb').read()
laststring = fulltxt.split(',')[-1]

This is not good for very big files because you load the complete text in memory but could be ok for small files. Note that laststring could include a newline character so strip it before use.

And finally if what the OP wants is the second string in line n (for n=2):

Update 2: This is now the same code than the one in the answer from J.F.Sebastian. (The credit is for him):

import csv
line_number = 2     
with open(myfilepath, 'rb') as f:
    mycsv = csv.reader(f)
    mycsv = list(mycsv)
    text = mycsv[line_number][1]
    ............
Community
  • 1
  • 1
joaquin
  • 82,968
  • 29
  • 138
  • 152
  • Note that `row[1]` is probably what you want, as indexing in Python is zero based. Also, you should always open CSV files in binary mode, as in `open(myfile, 'rb')` -- see [this answer](http://stackoverflow.com/questions/4249185/using-python-to-append-csv-files/4250021#4250021). Oh, and +1 for use of the CSV module (which, as Tyler pointed out, will handle quotes and whatnot properly). – Ben Hoyt Apr 22 '11 at 16:53
  • @benhoyt: I suppose, I wrote row[2] before the re-edition of the question. before it was `aaaaa@aaa.com,bbbbb ccccc@ccc.com,ddddd` – joaquin Apr 22 '11 at 16:58
  • @benhoyt: thnks for the link. I updated the answer and took note – joaquin Apr 22 '11 at 17:07
  • When I print row[1] I get all items of the 2nd column, but I only want one item, the 'ddddd'... – Joko Apr 22 '11 at 17:58
  • 1
    Maybe I wasn't clear enough. I need a way to get a specific item(field) of a CSV. Say I have a CSV with 100 rows and 2 columns (comma seperated). First column emails, second column passwords. For example I want to get the password of the email in row 38. So I need only the item from 2nd column row 38... – Joko Apr 22 '11 at 18:32
  • Look the new answer. Try to be more clear in your question next time. You can edit yourself your question to clarify it. – joaquin Apr 22 '11 at 18:34
  • Thank you very much. It's working fine now! And sorry for the misunderstanding... – Joko Apr 22 '11 at 18:55
  • 1
    Your code is unnecessary complicated. To extract a row with a given `line_number` you could `row = next(itertools.islice(csv.reader(f), line_number, line_number+1))` http://stackoverflow.com/questions/5757743/python-how-can-i-get-a-specific-field-of-a-csv-file/5758901#5758901 – jfs Apr 22 '11 at 19:07
  • @J.F Sebastian: tnks I know, the code has evolved trying to fit the different explanations from the OP. Please, post yourself your answer (I will vote for it :-)) I already posted a new answer trying to generalize the problem of the OP (asking for codes at different lines) – joaquin Apr 22 '11 at 19:14
8

There is an interesting point you need to catch about csv.reader() object. The csv.reader object is not list type, and not subscriptable.

This works:

for r in csv.reader(file_obj): # file not closed
    print r

This does not:

r = csv.reader(file_obj) 
print r[0]

So, you first have to convert to list type in order to make the above code work.

r = list( csv.reader(file_obj) )
print r[0]          
LBes
  • 3,366
  • 1
  • 32
  • 66
Surya
  • 4,824
  • 6
  • 38
  • 63
8
import csv

def read_cell(x, y):
    with open('file.csv', 'r') as f:
        reader = csv.reader(f)
        y_count = 0
        for n in reader:
            if y_count == y:
                cell = n[x]
                return cell
            y_count += 1

print (read_cell(4, 8)) 

This example prints cell 4, 8 in Python 3.

Turtles Are Cute
  • 3,200
  • 6
  • 30
  • 38
8
#!/usr/bin/env python
"""Print a field specified by row, column numbers from given csv file.

USAGE:
    %prog csv_filename row_number column_number
"""
import csv
import sys

filename = sys.argv[1]
row_number, column_number = [int(arg, 10)-1 for arg in sys.argv[2:])]

with open(filename, 'rb') as f:
     rows = list(csv.reader(f))
     print rows[row_number][column_number]

Example

$ python print-csv-field.py input.csv 2 2
ddddd

Note: list(csv.reader(f)) loads the whole file in memory. To avoid that you could use itertools:

import itertools
# ...
with open(filename, 'rb') as f:
     row = next(itertools.islice(csv.reader(f), row_number, row_number+1))
     print row[column_number]
jfs
  • 399,953
  • 195
  • 994
  • 1,670
1

Following may be be what you are looking for:

import pandas as pd

df = pd.read_csv("table.csv")

print(df["Password"][row_number])  

#where row_number is 38 maybe
Azhar Khan
  • 3,829
  • 11
  • 26
  • 32
0

Finaly I got it!!!

import csv

def select_index(index):
    csv_file = open('oscar_age_female.csv', 'r')
    csv_reader = csv.DictReader(csv_file)

    for line in csv_reader:
        l = line['Index']
        if l == index:
            print(line[' "Name"'])

select_index('11')

"Bette Davis"

ewokx
  • 2,204
  • 3
  • 14
  • 27
-1
import csv
inf = csv.reader(open('yourfile.csv','r'))
for row in inf:
  print row[1]
Tyler Eaves
  • 12,879
  • 1
  • 32
  • 39