Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #65533
| Path | csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <python@mrabarnett.plus.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'mrab': 0.05; 'output': 0.05; 'subject:text': 0.05; '(except': 0.07; 'element': 0.07; 'intermediate': 0.07; 'mentioned,': 0.07; 'subject:file': 0.07; 'wednesday,': 0.07; 'string': 0.09; '%s"': 0.09; 'bits': 0.09; 'rows,': 0.09; 'skip:% 20': 0.09; 'strings.': 0.09; 'subject:into': 0.09; 'python': 0.11; '2.7': 0.14; '"does': 0.16; '%s\\n"': 0.16; 'callable': 0.16; 'comma': 0.16; 'csv': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'indexerror:': 0.16; 'item)': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'received:192.168.1.4': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'repr': 0.16; 'roy': 0.16; 'thought.': 0.16; 'tuple': 0.16; 'tuple,': 0.16; 'wanted.': 0.16; 'work"': 0.16; 'index': 0.16; 'skip:= 10': 0.16; ':-)': 0.16; 'all.': 0.16; 'wrote:': 0.18; 'variable': 0.18; 'trying': 0.19; 'written': 0.21; 'seems': 0.21; 'import': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; 'error': 0.23; 'skip:% 10': 0.24; 'tells': 0.24; 'why.': 0.24; 'sort': 0.25; 'header:In- Reply-To:1': 0.27; "i'm": 0.30; 'work.': 0.31; 'gives': 0.31; 'code': 0.31; 'prints': 0.31; 'question:': 0.31; 'values.': 0.31; 'file': 0.32; 'figure': 0.32; 'run': 0.32; 'text': 0.33; 'running': 0.33; 'reader': 0.33; 'something': 0.35; 'received:84': 0.35; 'but': 0.35; 'list': 0.37; 'easily': 0.37; 'being': 0.38; 'skip:o 20': 0.38; 'thank': 0.38; 'to:addr:python-list': 0.38; 'does': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'how': 0.40; 'location:': 0.61; 'entire': 0.61; 'range': 0.61; "you're": 0.61; 'making': 0.63; 'information': 0.63; 'term': 0.63; 'within': 0.65; 'here': 0.66; 'smith': 0.68; 'containing': 0.69; 'article': 0.77; 'yourself': 0.78; 'clearer': 0.84; 'execution.': 0.84; 'zhang': 0.84; 'reply,': 0.93 |
| X-CM-Score | 0.00 |
| X-CNFS-Analysis | v=2.1 cv=JLW1sq6b c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=sASEtNAQL0YA:10 a=bDB9ragH2TsA:10 a=ihvODaAuJD4A:10 a=IkcTkHD0fZMA:10 a=EBOSESyhAAAA:8 a=tJaGADJI-GUA:10 a=4RBUngkUAAAA:8 a=pGLkceISAAAA:8 a=9hcRhw1NRxjJTPjDjj4A:9 a=QEXdDO2ut3YA:10 a=Bm6qEjDGwGEA:10 a=MSl-tDqOz04A:10 |
| X-AUTH | mrabarnett:2500 |
| Date | Thu, 06 Feb 2014 13:16:37 +0000 |
| From | MRAB <python@mrabarnett.plus.com> |
| User-Agent | Mozilla/5.0 (Windows NT 6.3; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.3.0 |
| MIME-Version | 1.0 |
| To | python-list@python.org |
| Subject | Re: parse a csv file into a text file |
| References | <5c268845-003f-4e24-b27a-c89e9fbfcc6c@googlegroups.com> <roy-4D8195.19330005022014@news.panix.com> <b84f4c7e-eaeb-4827-bf12-5ea656e40bf3@googlegroups.com> |
| In-Reply-To | <b84f4c7e-eaeb-4827-bf12-5ea656e40bf3@googlegroups.com> |
| Content-Type | text/plain; charset=UTF-8; format=flowed |
| Content-Transfer-Encoding | 7bit |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.6444.1391692605.18130.python-list@python.org> (permalink) |
| Lines | 89 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1391692605 news.xs4all.nl 2946 [2001:888:2000:d::a6]:44140 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:65533 |
Show key headers only | View raw
On 2014-02-06 07:52, Zhen Zhang wrote:> 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
>
"line" is a list and within repr you're using (...) (parentheses)
instead of [...] (square brackets).
It might be clearer if you call the variable "row" because the CSV
reader returns rows, and each row is a list of strings.
> 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))
>
A 1-tuple (a tuple containing one item) is:
(item, )
It's actually the comma that makes it a tuple (except for the 0-tuple
"()"); it's just that it's often necessary to wrap it in (...), and
people then think it's those that are making it a tuple, but it's not!
> 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.
>
What is the value of "line" at that point?
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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