Re: Question on the csv library

2009-08-27 Thread vsoler
On Aug 27, 9:42 pm, Andreas Waldenburger use...@geekmail.invalid
wrote:
 On Thu, 27 Aug 2009 21:36:28 +0200 Andreas Waldenburger

 use...@geekmail.invalid wrote:
  [snip]

  Might I humbly suggest

   sheet = list(spamReader)  # ?

 Oh, and while I'm humbly suggesting:

 spam_reader instead of spamReader or SpamReader or SpamrEadeR or
 suchlike. Caps are reserved for classes.

 Not a necessity, of course. But it's the dialect around these parts.

 /W

 --
 INVALID? DE!

Thank you for your answers. Let me however make some comments:

1- the csv file was generated with Excel 2007; no prompts for what the
separator should be; Excel has used ; by default, without asking
anything

2- about capitalisation, I used the var spamReader because I just
copy/pasted from the official python site:

  http://docs.python.org/library/csv.html

3- when I try

 sheet = [row for row in spamReader]
 print sheet
[]

all I get is an empty list; something seems not to be working properly

Same result list: I get an empty list

 sheet = list(spamReader)

Thank you again for your help, which is highly appreciated.

Vicente Soler
-- 
http://mail.python.org/mailman/listinfo/python-list


Source code for csv module

2009-02-02 Thread vsoler
Hi you all,

I just discovered the csv module here in the comp.lang.python group.

I have found its manual, which is publicly available, but since I am
still a newby, learning techniques, I was wondering if the source code
for this module is available.

Is it possible to have a look at it?

Thanks
--
http://mail.python.org/mailman/listinfo/python-list


Re: Source code for csv module

2009-02-02 Thread vsoler
On 2 feb, 21:51, Jon Clements jon...@googlemail.com wrote:
 On 2 Feb, 20:46, vsoler vicente.so...@gmail.com wrote:

  Hi you all,

  I just discovered the csv module here in the comp.lang.python group.

  I have found its manual, which is publicly available, but since I am
  still a newby, learning techniques, I was wondering if the source code
  for this module is available.

  Is it possible to have a look at it?

  Thanks

 The csv module is a wrapper around a C extension. If you're happy
 reading C code then downloading the Python sources will let you take a
 goosey.

 Jon.

I'm still interested in learning python techniques. Are there any
other modules (standard or complementary) that I can use in my
education?
--
http://mail.python.org/mailman/listinfo/python-list


getting values from a text file (newby)

2009-02-01 Thread vsoler
Hi,

My foo.txt file contains the following:

1,house,2,5
2,table,6,7
3,chair,-4,5

... as seen with notepad.

This file was created with the OpenOffice Calc spreadsheet, but since
I use comma as the decimal separator for numbers, the last value in
each line appears sorrounded by quotes.

I would like to obtain:

[[1,house,2.5], [2,table,6.7], [3,chair,-4.5]]

in order to process the content of the file. However, so far, I have
not obtained the desired result.

I started testing with (only for first line):

f=open('foo.txt','r')
t=f.readline()
print t
t=t.split(',')
print t
f.close()

But what I am getting is far more complex than what I expected:

1,house,2,5

['1', 'house', '2', '5\n']

which is unprocessable.

Can anyboby help?
--
http://mail.python.org/mailman/listinfo/python-list


Re: getting values from a text file (newby)

2009-02-01 Thread vsoler
On 1 feb, 19:02, Stephen Hansen apt.shan...@gmail.com wrote:
 On Sun, Feb 1, 2009 at 9:24 AM, vsolervicente.so...@gmail.comwrote:Hi,
 My foo.txt file contains the following:
 1,house,2,5
 2,table,6,7
 3,chair,-4,5
 ... as seen with notepad.
 This file was created with the OpenOffice Calc spreadsheet, but since
 I use comma as the decimal separator for numbers, the last value in
 each line appears sorrounded by quotes.
 I would like to obtain:
 [[1,house,2.5], [2,table,6.7], [3,chair,-4.5]]
 If I read your requirements, right, I think you want:import csv
 data = []
 reader = csv.reader(open(filename, r))
 for line in reader:
  data.append(
  line[0], line[1], float(line[2].replace(,, .))
  )
 print data
 Although you may want to replace that last bit with
     decimal.Decimal(line[2].replace(,,.))
 If you want an exact result and not the approximate floating point result.
 Basically the csv module can read through the CSV file very easily, but 
 because you're using commas instead of points you just have to edit that out 
 before you convert it to a number.
 --S

  signature.asc
  1 KBVerDescargar

with small modifications, your answers work perfectly!!!

r: in the open statement, why do you use 'rb' as 2nd argument? b is
supposed to be binary, and my file is text!
Steve: your idea works
Stephen: I got an error message saying that append can only take one
argument while you add three; I have added [ ] around the three
arguments; now it's fine; I've also added int() around first argument
to turn it into an integer.

Thank you all indeed!
--
http://mail.python.org/mailman/listinfo/python-list


Re: getting values from a text file (newby)

2009-02-01 Thread vsoler
On 1 feb, 23:57, John Machin sjmac...@lexicon.net wrote:
 On Feb 2, 6:18 am, vsoler vicente.so...@gmail.com wrote:



  r: in the open statement, why do you use 'rb' as 2nd argument? b is
  supposed to be binary, and my file is text!

 Because unlike Stephen, r has read the csv manual. Binary mode is
 required to handle properly cases like '\n' embedded in a field --
 something which can easily happen when the data has been extracted
 from a database with slack data-entry validation.

Where can I get the csv manual?
--
http://mail.python.org/mailman/listinfo/python-list


Newby: how to transform text into lines of text

2009-01-25 Thread vsoler
Hello,

I'va read a text file into variable a

 a=open('FicheroTexto.txt','r')
 a.read()

a contains all the lines of the text separated by '\n' characters.

Now, I want to work with each line separately, without the '\n'
character.

How can I get variable b as a list of such lines?

Thank you for your help
--
http://mail.python.org/mailman/listinfo/python-list


Re: Newby: how to transform text into lines of text

2009-01-25 Thread vsoler
On 25 ene, 14:36, Diez B. Roggisch de...@nospam.web.de wrote:
 vsoler schrieb:

  Hello,

  I'va read a text file into variable a

       a=open('FicheroTexto.txt','r')
       a.read()

  a contains all the lines of the text separated by '\n' characters.

 No, it doesn't. a.read() *returns* the contents, but you don't assign
 it, so it is discarded.

  Now, I want to work with each line separately, without the '\n'
  character.

  How can I get variable b as a list of such lines?

 The idiomatic way would be iterating over the file-object itself - which
 will get you the lines:

 with open(foo.txt) as inf:
      for line in inf:
          print line

 The advantage is that this works even for large files that otherwise
 won't fit into memory. Your approach of reading the full contents can be
 used like this:

 content = a.read()
 for line in content.split(\n):
      print line

 Diez

Thanks a lot. Very quick and clear
--
http://mail.python.org/mailman/listinfo/python-list


<    1   2