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


Groups > comp.lang.python > #65497

Re:parse a csv file into a text file

Path csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.003
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'subject:text': 0.05; 'column': 0.07; 'defaults': 0.07; 'subject:file': 0.07; 'string': 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; 'wrote': 0.14; '+b,': 0.16; 'columns': 0.16; 'csv': 0.16; 'least.': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'wanted.': 0.16; 'import': 0.22; 'print': 0.22; 'parse': 0.24; 'file.': 0.24; 'looks': 0.24; 'second': 0.26; 'header:X -Complaints-To:1': 0.27; 'file': 0.32; 'text': 0.33; 'reader': 0.33; 'could': 0.34; 'add': 0.35; 'hi,': 0.36; 'too': 0.37; 'skip:o 20': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'easy': 0.60; '5th': 0.60; "you're": 0.61; 'save': 0.62; 'different': 0.65; 'between': 0.67; '1st': 0.74; 'zhang': 0.84; 'subject::': 0.85; 'montreal': 0.91; 'toronto': 0.91; 'hill': 0.95
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Dave Angel <davea@davea.name>
Subject Re:parse a csv file into a text file
Date Wed, 5 Feb 2014 19:57:26 -0500 (EST)
Organization news.gmane.org
References <5c268845-003f-4e24-b27a-c89e9fbfcc6c@googlegroups.com>
X-Gmane-NNTP-Posting-Host dpc6744192085.direcpc.com
X-Newsreader PiaoHong Usenet NewsReaders 1.36
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.6431.1391648067.18130.python-list@python.org> (permalink)
Lines 64
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1391648067 news.xs4all.nl 2914 [2001:888:2000:d::a6]:34806
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:65497

Show key headers only | View raw


 Zhen Zhang <zhen.zhang.uoft@gmail.com> Wrote in message:
> 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.

Looks to me like columns 1 and 6.

> 
> 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])

Why not use print to file f? The approach for redirection is
 different between python 2 and 3, and you neglected to say which
 you're using. 
> 

> 
> My thinking is that I could add those 2 string together like c=a+' ' +b, that would give me the format i wanted.

And don't forget the "\n" at end of line. 

> So i can use f.write() to write into a file  ;) 

Or use print, which defaults to adding in a newline.

> Sorry if my questions sounds too easy or stupid.
> 

Not in the least.

> 
> 


-- 
DaveA

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