Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #65495
| Path | csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <python.list@tim.thechases.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; 'output': 0.05; 'subject:text': 0.05; '(using': 0.07; 'column': 0.07; 'subject:file': 0.07; 'string': 0.09; '"("': 0.09; 'bits': 0.09; 'character,': 0.09; 'skip:% 20': 0.09; 'subject:into': 0.09; 'python': 0.11; 'assume': 0.14; '%s\\n"': 0.16; '-tkc': 0.16; '16:10,': 0.16; 'attends': 0.16; 'csv': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'get,': 0.16; 'naming': 0.16; 'parameters,': 0.16; ':-)': 0.16; 'wrote:': 0.18; 'trying': 0.19; 'passing': 0.19; 'pieces': 0.19; 'split': 0.19; 'import': 0.22; 'stopping': 0.24; 'string,': 0.24; 'file.': 0.24; 'second': 0.26; 'header:In-Reply-To:1': 0.27; 'idea': 0.28; 'function': 0.29; 'work.': 0.31; 'extract': 0.31; 'file:': 0.31; 'strip': 0.31; 'file': 0.32; 'reader': 0.33; "i'd": 0.34; 'could': 0.34; 'problem': 0.35; 'something': 0.35; 'convert': 0.35; 'but': 0.35; 'otherwise.': 0.36; 'doing': 0.36; 'method': 0.36; 'charset:us- ascii': 0.36; 'skip:o 20': 0.38; 'to:addr:python-list': 0.38; 'rather': 0.38; 'to:addr:python.org': 0.39; 'how': 0.40; 'remove': 0.60; 'location:': 0.61; "you're": 0.61; 'first': 0.61; 'invalid': 0.68; '"it': 0.84; 'career.': 0.84; 'received:50.22': 0.84; 'zhang': 0.84; 'toronto': 0.91 |
| Date | Wed, 5 Feb 2014 18:46:04 -0600 |
| From | Tim Chase <python.list@tim.thechases.com> |
| To | python-list@python.org |
| Subject | Re: parse a csv file into a text file |
| In-Reply-To | <5c268845-003f-4e24-b27a-c89e9fbfcc6c@googlegroups.com> |
| References | <5c268845-003f-4e24-b27a-c89e9fbfcc6c@googlegroups.com> |
| X-Mailer | Claws Mail 3.8.1 (GTK+ 2.24.10; x86_64-pc-linux-gnu) |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=US-ASCII |
| Content-Transfer-Encoding | 7bit |
| X-Authenticated-Sender | tim@thechases.com |
| X-AntiAbuse | This header was added to track abuse, please include it with any abuse report |
| X-AntiAbuse | Primary Hostname - boston.accountservergroup.com |
| X-AntiAbuse | Original Domain - python.org |
| X-AntiAbuse | Originator/Caller UID/GID - [47 12] / [47 12] |
| X-AntiAbuse | Sender Address Domain - tim.thechases.com |
| X-Get-Message-Sender-Via | boston.accountservergroup.com: authenticated_id: tim@thechases.com |
| 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.6429.1391647532.18130.python-list@python.org> (permalink) |
| Lines | 59 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1391647532 news.xs4all.nl 2960 [2001:888:2000:d::a6]:56346 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:65495 |
Show key headers only | View raw
On 2014-02-05 16:10, Zhen Zhang wrote:
> import csv
> file = open('raw.csv')
Asaf recommended using string methods to split the file. Keep doing
what you're doing (using the csv module), as it attends to a lot of
edge-cases that will trip you up otherwise. I learned this the hard
way several years into my Python career. :-)
> reader = csv.reader(file)
>
> f = open('NicelyDone.text','w')
>
> for line in reader:
> f.write("%s %s"%line[1],%line[5])
Here, I'd start by naming the pieces that you get, so do
for line in reader:
location = line[1]
value = line[5]
> Also, I have to process the first column eg, "Toronto (Ont.)" into
> "Toronto". I am familiar with the function find(), I assume that i
> could extract Toronto out of Toronto(Ont.) using "(" as the
> stopping character, but based on my research , I have no idea how
> to use it and ask it to return me the string(Toronto).
You can use the .split() method to split a string, so you could do
something like
if '(' in location:
bits = location.split('(')
# at this point, bits = ['Toronto ', 'Ont.)']
location = bits[0].strip() # also strip it to remove whitespace
> 1:What is the data format for line[1], if it is string how come
> f.write()does not work. if it is not string, how do i convert it to
> a string?
The problem is not that "it is not a string" but that you passing
multiple parameters, the second of which is invalid Python because it
has an extra percent-sign. First create the one string that you
want to output:
output = "%s %s\n" % (location, bits)
and then write it out to the file:
f.write(output)
rather than trying to do it all in one pass.
-tkc
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