Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #65491 > unrolled thread
| Started by | Zhen Zhang <zhen.zhang.uoft@gmail.com> |
|---|---|
| First post | 2014-02-05 16:10 -0800 |
| Last post | 2014-02-06 12:51 -0600 |
| Articles | 10 on this page of 30 — 12 participants |
Back to article view | Back to comp.lang.python
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
Page 2 of 2 — ← Prev page 1 [2]
| From | Jussi Piitulainen <jpiitula@ling.helsinki.fi> |
|---|---|
| Date | 2014-02-06 10:49 +0200 |
| Message-ID | <qoty51oaamg.fsf@ruuvi.it.helsinki.fi> |
| In reply to | #65519 |
Zhen Zhang writes:
...
> I am currently running python 2.7.
>
> Yes, i thought there must be a print function in python like fprint
> in C++ that allows you to print into a file directly.
>
> But i google about "print string into text file" I got answers using
> f.write() instead. :)
Indeed. The first Python hit for me with that query was the tutorial
page on I/O in Python 2, and it does exactly that.
<http://docs.python.org/2/tutorial/inputoutput.html>
That page does refer to the spec of the print statement, where you can
find the way to redirect the output to a file, but you need to be able
to read formal syntax specifications like this:
print_stmt ::= "print" ([expression ("," expression)* [","]]
| ">>" expression [("," expression)+ [","]])
The relevant pattern is the second alternative, after the vertical
bar, which can be instantiated this way:
print >> f, e0, e1
There is one object f with a .write method, and one or more
expressions whose values get written using f.write; the effect of an
optional comma at end is also specified there. Not tutorial-level.
<http://docs.python.org/2/reference/simple_stmts.html#print>
But I use the newer print function even if I have to use 2.7,
something like this:
from __future__ import print_function
f = open("test.txt", "w")
print("hello?", "see me?", file=f)
f.close()
It does a modest amount of formatting: the value of the keyword
argument sep is written between the values, and the value of end is
written at end.
[toc] | [prev] | [next] | [standalone]
| From | Dave Angel <davea@davea.name> |
|---|---|
| Date | 2014-02-06 06:35 -0500 |
| Message-ID | <mailman.6442.1391686335.18130.python-list@python.org> |
| In reply to | #65519 |
Zhen Zhang <zhen.zhang.uoft@gmail.com> Wrote in message:
>
> I am currently running python 2.7.
>
> Yes, i thought there must be a print function in python like fprint in C++ that allows you to print into a file directly.
> But i google about "print string into text file" I got answers using f.write() instead. :)
> --
>
>
In python 2.x,
Instead of
f.write (a + " " + b)
you can use
print >> f, a, b
--
DaveA
[toc] | [prev] | [next] | [standalone]
| From | Dave Angel <davea@davea.name> |
|---|---|
| Date | 2014-02-06 06:53 -0500 |
| Message-ID | <mailman.6443.1391687418.18130.python-list@python.org> |
| In reply to | #65519 |
Dave Angel <davea@davea.name> Wrote in message:
> Zhen Zhang <zhen.zhang.uoft@gmail.com> Wrote in message:
>>
>
>> I am currently running python 2.7.
>>
>> Yes, i thought there must be a print function in python like fprint in C++ that allows you to print into a file directly.
>> But i google about "print string into text file" I got answers using f.write() instead. :)
>> --
>>
Oops. Forgot the newline.
> In python 2.x,
>
> Instead of
> f.write (a + " " + b)
f.write (a + " " + b + "\n")
> you can use
> print >> f, a, b
>
print will add in the space and newline, just as it does to
sys.stdout.
--
DaveA
[toc] | [prev] | [next] | [standalone]
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Date | 2014-02-05 22:01 -0500 |
| Message-ID | <mailman.6432.1391655695.18130.python-list@python.org> |
| In reply to | #65491 |
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,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])
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
and paste after reading it carefully.
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.
>
> 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).
>
> 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
>
--
Terry Jan Reedy
[toc] | [prev] | [next] | [standalone]
| From | Neil Cerutti <neilc@norwich.edu> |
|---|---|
| Date | 2014-02-06 14:02 +0000 |
| Message-ID | <mailman.6446.1391695381.18130.python-list@python.org> |
| In reply to | #65491 |
On 2014-02-06, Zhen Zhang <zhen.zhang.uoft@gmail.com> 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
[...]
into a text file like the following
>
> Toronto 2503281
[...]
> This is what i have so far.
>
>
> [code]
>
> import csv
> file = open('raw.csv')
You must open the file in binary mode, as that is what the csv
module expects in Python 2.7. newline handling can be enscrewed
if you forget.
file = open('raw.csv', 'b')
--
Neil Cerutti
[toc] | [prev] | [next] | [standalone]
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Date | 2014-02-06 17:40 +0000 |
| Message-ID | <mailman.6448.1391708444.18130.python-list@python.org> |
| In reply to | #65491 |
On 06/02/2014 14:02, Neil Cerutti wrote:
>
> You must open the file in binary mode, as that is what the csv
> module expects in Python 2.7. newline handling can be enscrewed
> if you forget.
>
> file = open('raw.csv', 'b')
>
I've never opened a file in binary mode to read with the csv module
using any Python version. Where does it state that you must do this?
--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.
Mark Lawrence
[toc] | [prev] | [next] | [standalone]
| From | Tim Chase <python.list@tim.thechases.com> |
|---|---|
| Date | 2014-02-06 11:51 -0600 |
| Message-ID | <mailman.6449.1391709038.18130.python-list@python.org> |
| In reply to | #65491 |
On 2014-02-06 17:40, Mark Lawrence wrote:
> On 06/02/2014 14:02, Neil Cerutti wrote:
> >
> > You must open the file in binary mode, as that is what the csv
> > module expects in Python 2.7. newline handling can be enscrewed
> > if you forget.
> >
> > file = open('raw.csv', 'b')
> >
>
> I've never opened a file in binary mode to read with the csv module
> using any Python version. Where does it state that you must do
> this?
While the docs don't currently say anything about it, all the
examples at [1] use 'rb' or 'wb' when opening the file. I've long
wondered about that. Especially as I've passed non-file objects like
lists/iterators to the csv.reader/csv.DictReader and had them work
just fine (and would be a little perturbed if they broke).
-tkc
[1] http://docs.python.org/2/library/csv.html
[toc] | [prev] | [next] | [standalone]
| From | Tim Golden <mail@timgolden.me.uk> |
|---|---|
| Date | 2014-02-06 18:05 +0000 |
| Message-ID | <mailman.6451.1391709913.18130.python-list@python.org> |
| In reply to | #65491 |
On 06/02/2014 17:40, Mark Lawrence wrote:
> On 06/02/2014 14:02, Neil Cerutti wrote:
>>
>> You must open the file in binary mode, as that is what the csv
>> module expects in Python 2.7. newline handling can be enscrewed
>> if you forget.
>>
>> file = open('raw.csv', 'b')
>>
>
> I've never opened a file in binary mode to read with the csv module
> using any Python version. Where does it state that you must do this?
>
If you don't, you tend to get interleaved blank lines. (Presumably
unless your .csv is using \n-only linefeeds).
TJG
[toc] | [prev] | [next] | [standalone]
| From | Neil Cerutti <neilc@norwich.edu> |
|---|---|
| Date | 2014-02-06 18:34 +0000 |
| Message-ID | <mailman.6453.1391711723.18130.python-list@python.org> |
| In reply to | #65491 |
On 2014-02-06, Tim Chase <python.list@tim.thechases.com> wrote:
> On 2014-02-06 17:40, Mark Lawrence wrote:
>> On 06/02/2014 14:02, Neil Cerutti wrote:
>> >
>> > You must open the file in binary mode, as that is what the csv
>> > module expects in Python 2.7. newline handling can be enscrewed
>> > if you forget.
>> >
>> > file = open('raw.csv', 'b')
>> >
>>
>> I've never opened a file in binary mode to read with the csv module
>> using any Python version. Where does it state that you must do
>> this?
>
> While the docs don't currently say anything about it, all the
> examples at [1] use 'rb' or 'wb' when opening the file. I've
> long wondered about that. Especially as I've passed non-file
> objects like lists/iterators to the csv.reader/csv.DictReader
> and had them work just fine (and would be a little perturbed if
> they broke).
They do actually mention it.
From: http://docs.python.org/2/library/csv.html
csv.reader(csvfile, dialect='excel', **fmtparams)
Return a reader object which will iterate over lines in the
given csvfile. csvfile can be any object which supports the
iterator protocol and returns a string each time its next()
method is called — file objects and list objects are both
suitable. If csvfile is a file object, it must be opened with
the ‘b’ flag on platforms where that makes a difference.
So it's stipulated only for file objects on systems where it
might make a difference.
--
Neil Cerutti
[toc] | [prev] | [next] | [standalone]
| From | Tim Chase <python.list@tim.thechases.com> |
|---|---|
| Date | 2014-02-06 12:51 -0600 |
| Message-ID | <mailman.6455.1391712654.18130.python-list@python.org> |
| In reply to | #65491 |
On 2014-02-06 18:34, Neil Cerutti wrote: > They do actually mention it. > > From: http://docs.python.org/2/library/csv.html > > If csvfile is a file object, it must be opened with > the ‘b’ flag on platforms where that makes a difference. > > So it's stipulated only for file objects on systems where it > might make a difference. Ah, I *knew* I'd read that somewhere but my searches in firefox (for "binary", "rb" and "wb") didn't manage to catch that particular instance. Thanks for disinterring that. -tkc
[toc] | [prev] | [standalone]
Page 2 of 2 — ← Prev page 1 [2]
Back to top | Article view | comp.lang.python
csiph-web