Skip to content Skip to sidebar Skip to footer

How Can I Get A Specific Field Of A Csv File?

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 wan

Solution 1:

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

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

import csv
withopen(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 = 2withopen(myfilepath, 'rb') as f:
    mycsv = csv.reader(f)
    mycsv = list(mycsv)
    text = mycsv[line_number][1]
    ............

Solution 2:

#!/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)-1for arg in sys.argv[2:])]

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

Example

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

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

import itertools
# ...withopen(filename, 'rb') as f:
     row = next(itertools.islice(csv.reader(f), row_number, row_number+1))
     print row[column_number]

Solution 3:

import csv

defread_cell(x, y):
    withopen('file.csv', 'r') as f:
        reader = csv.reader(f)
        y_count = 0for n in reader:
            if y_count == y:
                cell = n[x]
                return cell
            y_count += 1print (read_cell(4, 8)) 

This example prints cell 4, 8 in Python 3.

Solution 4:

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 closedprint 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]          

Solution 5:

Finaly I got it!!!

import csv

defselect_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"

Post a Comment for "How Can I Get A Specific Field Of A Csv File?"