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


Groups > comp.lang.python > #65513

Re: parse a csv file into a text file

Newsgroups comp.lang.python
Date 2014-02-05 23:52 -0800
References <5c268845-003f-4e24-b27a-c89e9fbfcc6c@googlegroups.com> <roy-4D8195.19330005022014@news.panix.com>
Message-ID <b84f4c7e-eaeb-4827-bf12-5ea656e40bf3@googlegroups.com> (permalink)
Subject Re: parse a csv file into a text file
From Zhen Zhang <zhen.zhang.uoft@gmail.com>

Show all headers | View raw


On Wednesday, February 5, 2014 7:33:00 PM UTC-5, Roy Smith wrote:
> In article <5c268845-003f-4e24-b27a-c89e9fbfcc6c@googlegroups.com>,
> 
>  Zhen Zhang <zhen.zhang.uoft@gmail.com> wrote:
> 
> 
> 
> > [code]
> 
> > 
> 
> > import csv
> 
> > file = open('raw.csv')
> 
> > reader = csv.reader(file)
> 
> > 
> 
> > f = open('NicelyDone.text','w')
> 
> > 
> 
> > for line in reader:
> 
> >       f.write("%s %s"%line[1],%line[5])
> 
> > 
> 
> > [/code]
> 
> 
> 
> Are you using Python 2 or 3?
> 
> 
> 
> > Here is my question:
> 
> > 1:What is the data format for line[1],
> 
> 
> 
> That's something you can easily figure out by printing out the 
> 
> intermediate values.  Try something like:
> 
> 
> 
> > for line in reader:
> 
> >       print type(line[1]), repr(line(1))
> 
> 
> 
> See if that prints what you expect.
> 
> 
> 
> > how come f.write() does not work.
> 
> 
> 
> What does "does not work" mean?  What does get written to the file?  Or 
> 
> do you get some sort of error?
> 
> 
> 
> I'm pretty sure I see your error, but I'm trying to lead you to being 
> 
> able to diagnose it yourself :-)

Hi Roy ,

Thank you so much for the reply,
I am currenly running python 2.7

i run the 
 print type(line[1]), repr(line(1)) 
It tells me that 'list object is not callable

It seems the entire line is a data type of list instead of a data type of "line" as i thought.

The line[1] is a string element of list after all.

f.write("%s %s %s" %(output,location,output))works great, 
as MRAB mentioned, I have to do write it in term of tuples.

This is the code I am currently using

for line in reader:
     location ="%s"%(line[1])
     if '(' in location:
        # at this point, bits = ['Toronto ', 'Ont.)'] 
        bits = location.split('(')  
        location = bits[0].strip()
     output = "%s %s\n" %(location,line[5])
     f.write("%s" %(output))

It extracts desired information into a text file as i wanted.
however, the python program gives me a Error after the execution.
 location="%s"%(line[1])
 IndexError: list index out of range

I failed to figure out why.

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


Thread

parse a csv file into a text file Zhen Zhang <zhen.zhang.uoft@gmail.com> - 2014-02-05 16:10 -0800
  Re: parse a csv file into a text file Asaf Las <roegltd@gmail.com> - 2014-02-05 16:17 -0800
    Re: parse a csv file into a text file Zhen Zhang <zhen.zhang.uoft@gmail.com> - 2014-02-05 23:56 -0800
  Re: parse a csv file into a text file Roy Smith <roy@panix.com> - 2014-02-05 19:33 -0500
    Re: parse a csv file into a text file Zhen Zhang <zhen.zhang.uoft@gmail.com> - 2014-02-05 23:52 -0800
      Re: parse a csv file into a text file Asaf Las <roegltd@gmail.com> - 2014-02-06 00:15 -0800
        Re: parse a csv file into a text file Asaf Las <roegltd@gmail.com> - 2014-02-06 00:48 -0800
      Re: parse a csv file into a text file MRAB <python@mrabarnett.plus.com> - 2014-02-06 13:16 +0000
        Re: parse a csv file into a text file Rustom Mody <rustompmody@gmail.com> - 2014-02-06 05:20 -0800
  Re: parse a csv file into a text file MRAB <python@mrabarnett.plus.com> - 2014-02-06 00:34 +0000
    Re: parse a csv file into a text file Zhen Zhang <zhen.zhang.uoft@gmail.com> - 2014-02-06 00:01 -0800
  Re: parse a csv file into a text file Tim Chase <python.list@tim.thechases.com> - 2014-02-05 18:46 -0600
    Re: parse a csv file into a text file Asaf Las <roegltd@gmail.com> - 2014-02-05 19:59 -0800
      Re: parse a csv file into a text file Tim Chase <python.list@tim.thechases.com> - 2014-02-05 22:09 -0600
        Re: parse a csv file into a text file Asaf Las <roegltd@gmail.com> - 2014-02-05 20:17 -0800
    Re: parse a csv file into a text file Zhen Zhang <zhen.zhang.uoft@gmail.com> - 2014-02-06 00:07 -0800
      Re: parse a csv file into a text file Tim Chase <python.list@tim.thechases.com> - 2014-02-06 12:49 -0600
  Re: parse a csv file into a text file Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-02-06 00:50 +0000
  Re:parse a csv file into a text file Dave Angel <davea@davea.name> - 2014-02-05 19:57 -0500
    Re: parse a csv file into a text file Zhen Zhang <zhen.zhang.uoft@gmail.com> - 2014-02-06 00:12 -0800
      Re: parse a csv file into a text file Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2014-02-06 10:49 +0200
      Re: parse a csv file into a text file Dave Angel <davea@davea.name> - 2014-02-06 06:35 -0500
      Re: parse a csv file into a text file Dave Angel <davea@davea.name> - 2014-02-06 06:53 -0500
  Re: parse a csv file into a text file Terry Reedy <tjreedy@udel.edu> - 2014-02-05 22:01 -0500
  Re: parse a csv file into a text file Neil Cerutti <neilc@norwich.edu> - 2014-02-06 14:02 +0000
  Re: parse a csv file into a text file Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-02-06 17:40 +0000
  Re: parse a csv file into a text file Tim Chase <python.list@tim.thechases.com> - 2014-02-06 11:51 -0600
  Re: parse a csv file into a text file Tim Golden <mail@timgolden.me.uk> - 2014-02-06 18:05 +0000
  Re: parse a csv file into a text file Neil Cerutti <neilc@norwich.edu> - 2014-02-06 18:34 +0000
  Re: parse a csv file into a text file Tim Chase <python.list@tim.thechases.com> - 2014-02-06 12:51 -0600

csiph-web