Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get a specific field of a csv file?

Tags:

python

csv

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...

like image 538
Joko Avatar asked Apr 22 '11 16:04

Joko


2 Answers

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]     ............ 
like image 168
joaquin Avatar answered Sep 25 '22 06:09

joaquin


#!/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] 
like image 44
jfs Avatar answered Sep 25 '22 06:09

jfs