Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #54934

Re: What's the best way to extract 2 values from a CSV file from each row systematically?

Newsgroups comp.lang.python
Date 2013-09-28 03:21 -0700
References <fbfc360b-0553-4203-b89a-80b4c4036aec@googlegroups.com>
Message-ID <3cc3a7eb-4ff1-41b6-963d-edb85b927949@googlegroups.com> (permalink)
Subject Re: What's the best way to extract 2 values from a CSV file from each row systematically?
From Luca Cerone <luca.cerone@gmail.com>

Show all headers | View raw


> I'd really appreciate any suggestions or help, thanks in advance!

Hi Alex if you know that you want only columns 3 and 5, you could also use list comprehension to fetch the values:

import csv

with open('yourfile.csv','rU') as fo: 
 #the rU means read using Universal newlines
 cr = csv.reader(fo)
 values_list = [(r[2],r[4]) for r in cr] #you have a list of tuples containing the values you need

Cheers,
Luca

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

What's the best way to extract 2 values from a CSV file from each row systematically? quarantinemiles@gmail.com - 2013-09-23 10:10 -0700
  Re: What's the best way to extract 2 values from a CSV file from each row systematically? Neil Cerutti <neilc@norwich.edu> - 2013-09-23 17:20 +0000
  Re: What's the best way to extract 2 values from a CSV file from each row systematically? Joel Goldstick <joel.goldstick@gmail.com> - 2013-09-23 13:25 -0400
  Re: What's the best way to extract 2 values from a CSV file from each row systematically? Tim Chase <python.list@tim.thechases.com> - 2013-09-23 12:47 -0500
    Re: What's the best way to extract 2 values from a CSV file from each row systematically? Alex Lee <quarantinemiles@gmail.com> - 2013-09-24 07:04 -0700
      Re: What's the best way to extract 2 values from a CSV file from each row systematically? Roland Mueller <roland.em0001@googlemail.com> - 2013-09-27 12:22 +0300
  Re: What's the best way to extract 2 values from a CSV file from each row systematically? Luca Cerone <luca.cerone@gmail.com> - 2013-09-28 03:21 -0700

csiph-web