Path: csiph.com!usenet.pasdenom.info!gegeweb.org!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'subject:text': 0.05; 'column': 0.07; 'subject:file': 0.07; 'string': 0.09; '"("': 0.09; 'character,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'skip:% 20': 0.09; 'subject:into': 0.09; 'python': 0.11; 'jan': 0.12; 'assume': 0.14; '%s\\n"': 0.16; '+b,': 0.16; 'carefully.': 0.16; 'csv': 0.16; 'out)': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'wanted.': 0.16; 'wrote:': 0.18; 'working.': 0.19; 'import': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; 'parse': 0.24; 'stopping': 0.24; 'string,': 0.24; 'file.': 0.24; 'second': 0.26; 'header:X -Complaints-To:1': 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; 'text': 0.33; 'reader': 0.33; 'could': 0.34; 'something': 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; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'easy': 0.60; '5th': 0.60; 'received:173': 0.61; 'first': 0.61; 'save': 0.62; 'here': 0.66; 'dont': 0.67; '1st': 0.74; 'cut': 0.74; 'received:fios.verizon.net': 0.84; 'zhang': 0.84; 'montreal': 0.91; 'toronto': 0.91; 'hill': 0.95 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: parse a csv file into a text file Date: Wed, 05 Feb 2014 22:01:06 -0500 References: <5c268845-003f-4e24-b27a-c89e9fbfcc6c@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: quoted-printable X-Gmane-NNTP-Posting-Host: pool-173-75-254-207.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.3.0 In-Reply-To: <5c268845-003f-4e24-b27a-c89e9fbfcc6c@googlegroups.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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 82 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1391655695 news.xs4all.nl 2831 [2001:888:2000:d::a6]:40604 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:65505 On 2/5/2014 7:10 PM, 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,6= 30.1763,3972.4,1 > 2466023,"Montr=C3=A9al (Que.)",V ,F,1620693,1583590,T,F,2.3,787060,743= 204,365.1303,4438.7,2 > 5915022,"Vancouver (B.C.)",CY ,F,578041,545671,F,F,5.9,273804,253212,11= 4.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 =3D open('raw.csv') > reader =3D csv.reader(file) > > f =3D open('NicelyDone.text','w') > > for line in reader: > f.write("%s %s"%line[1],%line[5]) f.write("%s %s\n" % (line[1], line[5])) should do better. > > [/code] > > This is not working for me, Always say how something is not working. If there is a traceback, cut=20 and paste after reading it carefully. I was able to extract the data from the csv file as line[1],line[5].=20 (I am able to print it out) > But I dont know how to write it to a .text file in the format i wanted.= > > Also, I have to process the first column eg, "Toronto (Ont.)" into "Tor= onto". > I am familiar with the function find(), I assume that i could extract T= oronto 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 r= eturn me the string(Toronto). > > 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 f= orm using find() or other methods. > > My thinking is that I could add those 2 string together like c=3Da+' ' = +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 > --=20 Terry Jan Reedy