Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #65494
| Path | csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!news.mixmin.net!news2.arglkargh.de!news.karotte.org!news.space.net!news.osn.de!diablo2.news.osn.de!news.tele.dk!news.tele.dk!small.news.tele.dk!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; 'argument': 0.05; 'subject:text': 0.05; 'column': 0.07; 'method.': 0.07; 'subject:file': 0.07; 'string': 0.09; '"("': 0.09; '%s"': 0.09; '-1)': 0.09; 'arguments': 0.09; 'character,': 0.09; 'skip:% 20': 0.09; 'subject:into': 0.09; 'python': 0.11; 'assume': 0.14; '+b,': 0.16; 'argument:': 0.16; 'csv': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'left,': 0.16; 'message- id:@mrabarnett.plus.com': 0.16; 'operator.': 0.16; 'out)': 0.16; 'received:192.168.1.4': 0.16; 'tuple,': 0.16; 'wanted.': 0.16; 'index': 0.16; 'wrote:': 0.18; 'import': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; 'parse': 0.24; 'stopping': 0.24; 'string,': 0.24; 'file.': 0.24; 'right.': 0.26; 'second': 0.26; 'skip:" 20': 0.27; 'header:In-Reply-To:1': 0.27; 'idea': 0.28; 'function': 0.29; 'work.': 0.31; 'extract': 0.31; 'question:': 0.31; 'file': 0.32; 'another': 0.32; 'text': 0.33; 'reader': 0.33; 'could': 0.34; 'case,': 0.35; 'convert': 0.35; 'but': 0.35; 'add': 0.35; 'there': 0.35; 'thanks': 0.36; 'hi,': 0.36; 'should': 0.36; 'too': 0.37; 'skip:o 20': 0.38; 'ahead': 0.38; 'to:addr:python- list': 0.38; 'to:addr:python.org': 0.39; 'how': 0.40; 'easy': 0.60; '5th': 0.60; 'tell': 0.60; 'first': 0.61; 'save': 0.62; 'here': 0.66; 'dont': 0.67; '1st': 0.74; 'zhang': 0.84; 'montreal': 0.91; 'toronto': 0.91; 'hill': 0.95 |
| X-CM-Score | 0.00 |
| X-CNFS-Analysis | v=2.1 cv=eZmzft0H c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=5FYZ9MsUIQAA:10 a=bDB9ragH2TsA:10 a=ihvODaAuJD4A:10 a=IkcTkHD0fZMA:10 a=EBOSESyhAAAA:8 a=tJaGADJI-GUA:10 a=CH6PCR5Igp4oBQhpDsgA:9 a=QEXdDO2ut3YA:10 |
| X-AUTH | mrabarnett:2500 |
| Date | Thu, 06 Feb 2014 00:34:57 +0000 |
| From | MRAB <python@mrabarnett.plus.com> |
| User-Agent | Mozilla/5.0 (Windows NT 6.3; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.2.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> |
| In-Reply-To | <5c268845-003f-4e24-b27a-c89e9fbfcc6c@googlegroups.com> |
| Content-Type | text/plain; charset=UTF-8; format=flowed |
| Content-Transfer-Encoding | 8bit |
| 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.6428.1391646904.18130.python-list@python.org> (permalink) |
| Lines | 72 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1391646904 news.xs4all.nl 2978 [2001:888:2000:d::a6]:47310 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:65494 |
Show key headers only | View raw
On 2014-02-06 00:10, Zhen Zhang wrote:
> Hi, every one.
>
> I am a second year EE student.
> I just started learning python for my project.
>
> I intend to parse a csv file with a format like
>
> 3520005,"Toronto (Ont.)",C ,F,2503281,2481494,F,F,0.9,1040597,979330,630.1763,3972.4,1
> 2466023,"Montréal (Que.)",V ,F,1620693,1583590,T,F,2.3,787060,743204,365.1303,4438.7,2
> 5915022,"Vancouver (B.C.)",CY ,F,578041,545671,F,F,5.9,273804,253212,114.7133,5039.0,8
> 3519038,"Richmond Hill (Ont.)",T ,F,162704,132030,F,F,23.2,53028,51000,100.8917,1612.7,28
>
> into a text file like the following
>
> Toronto 2503281
> Montreal 1620693
> Vancouver 578041
>
> I am extracting the 1st and 5th column and save it into a text file.
>
> This is what i have so far.
>
>
> [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]
>
> This is not working for me, I was able to extract the data from the csv file as line[1],line[5]. (I am able to print it out)
> But I dont know how to write it to a .text file in the format i wanted.
>
% is an operator. When used with a format string on its left, its
arguments go on its right. In the general case, those arguments should
be put in a tuple, although if there's only one argument and it's not a
tuple, you can write just that argument:
f.write("%s %s" % (line[1], 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).
>
Use find to tell you the index of the "(" (if there isn't one then
it'll return -1) and then slice the string to get the part preceding it.
Another way is to use the "partition" method.
Also, have a look at the "strip"/"lstrip"/"rstrip" methods.
> Here is my question:
> 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?
> 2:How do i extract the word Toronto out of Toronto(Ont) into a string form using find() or other methods.
>
> My thinking is that I could add those 2 string together like c=a+' ' +b, that would give me the format i wanted.
> So i can use f.write() to write into a file ;)
>
> Sorry if my questions sounds too easy or stupid.
>
> Thanks ahead
>
> Zhen
>
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