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:

[email protected],bbbbb
[email protected],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...

3
  • @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? Commented Apr 22, 2011 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. Commented Apr 22, 2011 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 Commented Apr 22, 2011 at 17:40

7 Answers 7

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]
    ............
Sign up to request clarification or add additional context in comments.

9 Comments

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. Oh, and +1 for use of the CSV module (which, as Tyler pointed out, will handle quotes and whatnot properly).
@benhoyt: I suppose, I wrote row[2] before the re-edition of the question. before it was [email protected],bbbbb [email protected],ddddd
@benhoyt: thnks for the link. I updated the answer and took note
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...
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)) stackoverflow.com/questions/5757743/…
|
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]

3 Comments

That still requires reading in all the lines until you get to where you want. The question is how to ONLY read what you want.
@Confounded: lines in csv may have a different length. There is no way to get the exact row_number without reading the file (note: islice version does not store the whole file in memory). To be able to seek (jump in the file) to the exact row number, you need a fixed format (binary format essentially) where you can predict the position inside the file (or don't care about precision)
And what is such file format? I am dealing with data (all double-precision floating-point format) that has multiple dimensions (i.e. a hyper-cube) and I am looking for ways to store and be able to access sections of that data. Thank you.
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.

Comments

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]          

Comments

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

Comments

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"

Comments

-1
import csv
inf = csv.reader(open('yourfile.csv','r'))
for row in inf:
  print row[1]

Comments

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.