Re: [Tutor] how to read from a txt file

2005-03-30 Thread Liam Clarke
So... you need those tabs? If you don't need them, go like this - 

 data_file = open(os.path.normpath(self.TextFile.GetValue()), 'r')
for x in data:
y = str(x)
   ( temp11, temp22, pyra11, pyra22, voltage11, current1) = y.split('\t')
  
And that should be all your values, separated in string format.




On Tue, 29 Mar 2005 20:56:16 -0800, jrlen balane [EMAIL PROTECTED] wrote:
 how should i modify this data reader:
 (assumes that there is only one entry per line followed by '\n')
 

 data = data_file.readlines()
 
 self.irradianceStrings = map(str, data)
 self.irradianceIntegers = map(int, data)
 self.IrradianceExecute.SetValue(''.join(self.irradianceStrings))
 
 so that i can read the text file created by this:
 
 self.filename = %s\%s.txt
 %(os.path.normpath(self.SaveFolder.GetValue()),time.strftime(%Y%m%d%H%M))
 
 self.table_file = open(self.filename,a)
 self.table_file.write('%f\t'%self.temp11)
 self.table_file.write('%f\t'%self.temp22)
 self.table_file.write('%f\t'%self.pyra11)
 self.table_file.write('%f\t'%self.pyra22)
 self.table_file.write('%f\t'%self.voltage11)
 self.table_file.write('%f\t'%self.current11)
 self.table_file.write('\n')
 self.table_file.close()
 
 
 On Tue, 15 Mar 2005 17:05:46 +1300, Liam Clarke [EMAIL PROTECTED] wrote:
  Whoops, golden rule - Never post untested code
  Sorry.
 
 
  On Mon, 14 Mar 2005 21:05:44 -0500, Kent Johnson [EMAIL PROTECTED] wrote:
   jrlen balane wrote:
ok, i've done what sir Kent just said, my fault...
   
but an error still occurs:
Traceback (most recent call last):
  File C:\Python23\practices\opentxtprintlngnew.py, line 18, in 
-toplevel-
print process(data)
  File C:\Python23\practices\opentxtprintlngnew.py, line 10, in 
process
tempLine = int(line)
ValueError: invalid literal for int(): abc
   
isn't this the job of :
   
except TypeError:
print Non numeric character in line, line
continue #Breaks, and starts with next line
  
   Yes, only it should be ValueError instead of TypeError. You can check 
   this interactively:
  int('foo')
   Traceback (most recent call last):
  File stdin, line 1, in ?
   ValueError: invalid literal for int(): foo
  
   Kent
  
   ___
   Tutor maillist  -  Tutor@python.org
   http://mail.python.org/mailman/listinfo/tutor
  
 
  --
  'There is only one basic human right, and that is to do as you damn well 
  please.
  And with it comes the only basic human duty, to take the consequences.
  ___
  Tutor maillist  -  Tutor@python.org
  http://mail.python.org/mailman/listinfo/tutor
 
 


-- 
'There is only one basic human right, and that is to do as you damn well please.
And with it comes the only basic human duty, to take the consequences.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to read from a txt file

2005-03-30 Thread Liam Clarke
 print temp1[x], temp2[x]

This won't work. 

 fob = []
 gab = [fooBar,Baz,aBBa]
 for line in gab:
... print line, 
... x = line.replace('B', 'X')
... print x
... fob.append(x)
... print fob[line]
... 
fooBar fooXar
Traceback (most recent call last):
  File interactive input, line 6, in ?
TypeError: list indices must be integers

ValueError: unpack list of wrong size
What should I do?

Catch the exception - 

try:
  (temp11, temp22, pyra11, pyra22, voltage11, current11) = y.split('\t')
except ValueError:
   print Line:, y
   print len(y.split('\t')), items found
 
And see what's going wrong. You get a ValueError like that one like so - 
 x = [1,2,3,
   4,5,6,
7,8]
 for af in x:
... (a,b,c) = af.split(',')
... print a,b,c
... 
1 2 3
4 5 6
Traceback (most recent call last):
  File interactive input, line 2, in ?
ValueError: unpack list of wrong size

See, it's trying to get 3 items from each split, but the last one only
gives 2 items.

So, print the offending line, I'm guessing it's a blank \n  or \t line.

Regards, 

Liam Clarke


On Wed, 30 Mar 2005 17:08:07 +0800, jrlen balane [EMAIL PROTECTED] wrote:
 after running this in IDLE:
 
 import sys
 import serial
 import sys, os
 import serial
 import string
 import time
 from struct import *
 
 temp1 = []
 temp2 = []
 pyra1 = []
 pyra2 = []
 voltage = []
 current = []
 
 data_file = open('C:/Documents and Settings/nyer/My
 Documents/Info/info2/200503300858.txt', 'r')
 data = data_file.readlines()
 for x in data:
 y = str(x)
 (temp11, temp22, pyra11, pyra22, voltage11, current11) = y.split('\t')
 temp11Integer = map(int, temp11)
 temp22Integer = map(int, temp22)
 pyra11Integer = map(int, pyra11)
 pyra22Integer = map(int, pyra22)
 voltage11Integer = map(int, voltage11)
 current11Integer = map(int, current11)
 
 print temp11Integer, temp22Integer, pyra11Integer, pyra22Integer,
 voltage11Integer, current11Integer
 
 temp1.append(temp11Integer)
 temp2.append(temp22Integer)
 pyra1.append(pyra11Integer)
 pyra2.append(pyra22Integer)
 voltage.append(voltage11Integer)
 current.append(current11Integer)
 
 print temp1[x], temp2[x]
 
 an error:
 Traceback (most recent call last):
   File C:/Python23/practices/read.py, line 21, in -toplevel-
 (temp11, temp22, pyra11, pyra22, voltage11, current11) = y.split('\t')
 ValueError: unpack list of wrong size
 
 What should i do??
 
 
 On Wed, 30 Mar 2005 00:29:05 -0800, jrlen balane [EMAIL PROTECTED] wrote:
  so basically, i'll just do this to append the data to the list:
 
  temp1[]=0
  temp2[]=0
  pyra1[] =0
  pyra2[] =0
  voltage[] =0
  current[] =0
 
  data_file = open(os.path.normpath(self.TextFile.GetValue()), 'r')
 
  for x in data:
y = str(x)
   ( temp11, temp22, pyra11, pyra22, voltage11, current11) = y.split('\t')
   temp11Integer = map(int, temp11)
   temp22Integer = map(int, temp22)
   pyra11Integer = map(int, pyra11)
   pyra22Integer = map(int, pyra22)
   voltage11Integer = map(int, voltage11)
   current11Integer = map(int, current11)
 
   temp1.append(temp11Integer)
   temp2.append(temp22Integer)
   pyra1.append(pyra11Integer)
   pyra2.append(pyra22Integer)
   voltage.append(voltage11Integer)
   current.append(current11Integer)
 
 
  On Wed, 30 Mar 2005 20:07:42 +1200, Liam Clarke [EMAIL PROTECTED] wrote:
   So... you need those tabs? If you don't need them, go like this -
  
data_file = open(os.path.normpath(self.TextFile.GetValue()), 'r')
   for x in data:
   y = str(x)
  ( temp11, temp22, pyra11, pyra22, voltage11, current1) = y.split('\t')
  
   And that should be all your values, separated in string format.
  
  
   On Tue, 29 Mar 2005 20:56:16 -0800, jrlen balane [EMAIL PROTECTED] 
   wrote:
how should i modify this data reader:
(assumes that there is only one entry per line followed by '\n')
   
  
data = data_file.readlines()
   
self.irradianceStrings = map(str, data)
self.irradianceIntegers = map(int, data)
self.IrradianceExecute.SetValue(''.join(self.irradianceStrings))
   
so that i can read the text file created by this:
   
self.filename = %s\%s.txt
%(os.path.normpath(self.SaveFolder.GetValue()),time.strftime(%Y%m%d%H%M))
   
self.table_file = open(self.filename,a)
self.table_file.write('%f\t'%self.temp11)
self.table_file.write('%f\t'%self.temp22)
self.table_file.write('%f\t'%self.pyra11)
self.table_file.write('%f\t'%self.pyra22)
self.table_file.write('%f\t'%self.voltage11)
self.table_file.write('%f\t'%self.current11)
self.table_file.write('\n')
self.table_file.close()
   
   
On Tue, 15 Mar 2005 17:05:46 +1300, Liam Clarke [EMAIL PROTECTED] 
wrote:
 Whoops, golden rule - Never post untested code
 Sorry.


 On Mon, 14 Mar 2005 21:05:44 -0500, Kent Johnson [EMAIL PROTECTED] 
 wrote:
  jrlen balane wrote:
   ok, i've done what sir Kent just said, my 

Re: [Tutor] how to read from a txt file

2005-03-29 Thread jrlen balane
am getting desperate on this, please help me, I just can't figure out
how to read those tabs

please help me!


On Tue, 29 Mar 2005 22:16:11 -0800, jrlen balane [EMAIL PROTECTED] wrote:
 I need the string representation of the data read so that i can put it
 on a wxGrid
 while i am goin to need the integer representation of the data so that
 i can plot it.
 
 anybody, please help!!!
 
 
 On Tue, 29 Mar 2005 20:56:16 -0800, jrlen balane [EMAIL PROTECTED] wrote:
  how should i modify this data reader:
  (assumes that there is only one entry per line followed by '\n')
 
  data_file = open(os.path.normpath(self.TextFile.GetValue()), 'r')
  data = data_file.readlines()
 
  self.irradianceStrings = map(str, data)
  self.irradianceIntegers = map(int, data)
  self.IrradianceExecute.SetValue(''.join(self.irradianceStrings))
 
  so that i can read the text file created by this:
 
  self.filename = %s\%s.txt
  %(os.path.normpath(self.SaveFolder.GetValue()),time.strftime(%Y%m%d%H%M))
 
  self.table_file = open(self.filename,a)
  self.table_file.write('%f\t'%self.temp11)
  self.table_file.write('%f\t'%self.temp22)
  self.table_file.write('%f\t'%self.pyra11)
  self.table_file.write('%f\t'%self.pyra22)
  self.table_file.write('%f\t'%self.voltage11)
  self.table_file.write('%f\t'%self.current11)
  self.table_file.write('\n')
  self.table_file.close()
 
 
  On Tue, 15 Mar 2005 17:05:46 +1300, Liam Clarke [EMAIL PROTECTED] wrote:
   Whoops, golden rule - Never post untested code
   Sorry.
  
  
   On Mon, 14 Mar 2005 21:05:44 -0500, Kent Johnson [EMAIL PROTECTED] 
   wrote:
jrlen balane wrote:
 ok, i've done what sir Kent just said, my fault...

 but an error still occurs:
 Traceback (most recent call last):
   File C:\Python23\practices\opentxtprintlngnew.py, line 18, in 
 -toplevel-
 print process(data)
   File C:\Python23\practices\opentxtprintlngnew.py, line 10, in 
 process
 tempLine = int(line)
 ValueError: invalid literal for int(): abc

 isn't this the job of :

 except TypeError:
 print Non numeric character in line, line
 continue #Breaks, and starts with next line
   
Yes, only it should be ValueError instead of TypeError. You can check 
this interactively:
   int('foo')
Traceback (most recent call last):
   File stdin, line 1, in ?
ValueError: invalid literal for int(): foo
   
Kent
   
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor
   
  
   --
   'There is only one basic human right, and that is to do as you damn well 
   please.
   And with it comes the only basic human duty, to take the consequences.
   ___
   Tutor maillist  -  Tutor@python.org
   http://mail.python.org/mailman/listinfo/tutor
  
 

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to read from a txt file

2005-03-14 Thread jrlen balane
say i have the code that reads decimal value from a text file:

import sys

data_file = open('C:/Documents and Settings/nyer/Desktop/nyer.txt', 'r')
data = data_file.readlines()

def process(list_of_lines):
data_points = []
for line in list_of_lines:
data_points.append(int(line))
return data_points

print process(data)


what if, on the text file, a user has encoded values other than
decimal, how would i add a code that would act like an Exception, or
would tell the user that there is an invalid entry in the text file,
like a letter or other character other than number ?


On Thu, 17 Feb 2005 01:44:19 -0800 (PST), Danny Yoo
[EMAIL PROTECTED] wrote:
 
 
   Traceback (most recent call last):
 File C:\Python23\practices\opentxt, line 12, in -toplevel-
   process(data)
 File C:\Python23\practices\opentxt, line 6, in process
   data_points.append(int(line))
   ValueError: invalid literal for int():
 
 Hi Brian,
 
 Ah, think about empty lines.
 
 Let's look at the error message again:
 
 ValueError: invalid literal for int():
   ^^^
 
 There's nothing visible there after the colon, and that's our hint.
 Notice what happens when we pass int()  some wacky strings:
 
 ###
  int(foobar)
 Traceback (most recent call last):
   File stdin, line 1, in ?
 ValueError: invalid literal for int(): foobar
 ###
 
 So whatever is being passed to int() should show up in the error message.
 This is exactly why getting literal error messages is so wonderful.
 *grin*
 
 Since we don't see anything here:
 
 File C:\Python23\practices\opentxt, line 12, in -toplevel-
   process(data)
 File C:\Python23\practices\opentxt, line 6, in process
   data_points.append(int(line))
   ValueError: invalid literal for int():
 
 my best guess is that there's an empty line in the file, since we get the
 same kind of error if we do this:
 
 ###
  int()
 Traceback (most recent call last):
   File stdin, line 1, in ?
 ValueError: invalid literal for int():
 ###
 
 Best of wishes to you!
 
 
 ___
 Tutor maillist  -  Tutor@python.org
 http://mail.python.org/mailman/listinfo/tutor

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to read from a txt file

2005-03-14 Thread Liam Clarke
Well, a string 12345 when called through int() will come back as 12345.

But, a string foo, called through int(), will raise a TypeError.

So

 import sys
 
 data_file = open('C:/Documents and Settings/nyer/Desktop/nyer.txt', 'r')
 data = data_file.readlines()
 
 def process(list_of_lines):
 data_points = []
 for line in list_of_lines:
 data_points.append(int(line))
 return data_points
 
 print process(data)

You could do this 

def process(list_of_lines):
 data_points=[]
 for line in list_of_lines:
   try:
   tempLine = int(line)
   except TypeError:
   print Non numeric character in line, line
   continue #Breaks, and starts with next line
   data_points.append(tempLine)


That's one way, but there's probably a better way.


Regards, 


Liam Clarke
-- 
'There is only one basic human right, and that is to do as you damn well please.
And with it comes the only basic human duty, to take the consequences.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to read from a txt file

2005-03-14 Thread jrlen balane
this is what i get after running this on IDLE:

import sys

data_file = open('C:/Documents and Settings/nyer/Desktop/nyer.txt', 'r')
data = data_file.readlines()

def process(list_of_lines):
data_points = []
for line in list_of_lines:
try:
  tempLine = int(line)
except TypeError:
  print Non numeric character in line, line
  continue #Breaks, and starts with next line

data_points.append(tempLine)
return data_points

print process(data)

=
[1000]

==
but this is what i have written on the text file:

1000
890
900
abc
500
650
850
1200
1100


On Tue, 15 Mar 2005 12:53:26 +1300, Liam Clarke [EMAIL PROTECTED] wrote:
 Oops, and I meant
 
 try:
tempLine = int(line)
 
 Silly indent error.
 
 
 On Tue, 15 Mar 2005 12:52:49 +1300, Liam Clarke [EMAIL PROTECTED] wrote:
  Well, a string 12345 when called through int() will come back as 12345.
 
  But, a string foo, called through int(), will raise a TypeError.
 
  So
 
   import sys
  
   data_file = open('C:/Documents and Settings/nyer/Desktop/nyer.txt', 'r')
   data = data_file.readlines()
  
   def process(list_of_lines):
   data_points = []
   for line in list_of_lines:
   data_points.append(int(line))
   return data_points
  
   print process(data)
 
  You could do this
 
  def process(list_of_lines):
   data_points=[]
   for line in list_of_lines:
 try:
 tempLine = int(line)
 except TypeError:
 print Non numeric character in line, line
 continue #Breaks, and starts with next line
 data_points.append(tempLine)
 
  That's one way, but there's probably a better way.
 
  Regards,
 
  Liam Clarke
  --
  'There is only one basic human right, and that is to do as you damn well 
  please.
  And with it comes the only basic human duty, to take the consequences.
 
 
 --
 'There is only one basic human right, and that is to do as you damn well 
 please.
 And with it comes the only basic human duty, to take the consequences.
 

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to read from a txt file

2005-03-14 Thread Kent Johnson
jrlen balane wrote:
this is what i get after running this on IDLE:
import sys
data_file = open('C:/Documents and Settings/nyer/Desktop/nyer.txt', 'r')
data = data_file.readlines()
def process(list_of_lines):
data_points = []
for line in list_of_lines:
try:
  tempLine = int(line)
except TypeError:
  print Non numeric character in line, line
  continue #Breaks, and starts with next line
data_points.append(tempLine)
return data_points
This line ^^^ is indented four spaces too much - you are returning after the first time through the 
loop. Indent it the same as the for statement and it will work correctly.

Kent
print process(data)
=
[1000]
==
but this is what i have written on the text file:
1000
890
900
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to read from a txt file

2005-02-17 Thread Brian van den Broek
jrlen balane said unto the world upon 2005-02-17 02:41:
sir, what seemed to be the problem with this:
def process(list_of_lines):
data_points = []
for line in list_of_lines:
data_points.append(int(line))
return data_points
data_file = open('C:/Documents and Settings/nyer/Desktop/nyer.txt', 'r')
data = data_file.readline()
print process(data)
here is what is written in the nyer.txt:
1000
890
900
500
650
850
1200
1100
what i want is to print data_points:
and this is the error:
Traceback (most recent call last):
  File C:\Python23\practices\opentxt, line 12, in -toplevel-
process(data)
  File C:\Python23\practices\opentxt, line 6, in process
data_points.append(int(line))
ValueError: invalid literal for int(): 
Hi,
I think the traceback is my fault from an oversight in the code I sent 
you when you posted before. Sorry about that :-[

There are two problems with your code.
The immediate one, due to my advice, is that each line of your file 
ends with a newline character ('\n'). So, you cannot call int on '1000\n'.

Try
data_points.append(int(line[:-1]))
instead. That will call int on line minus the last character (the 
newline).
 	
The other problem is that you use data = data_file.readline(). That 
will give you a single line each time you call it. If you stick with 
this approach, I think you want data = data_file.readlines() (note the 
's' at the end.)

But, you might do well to consider some of the other suggestions you 
got. They came from more capable programmers than me!

Best,
Brian vdB
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to read from a txt file

2005-02-17 Thread Brian van den Broek
Brian van den Broek said unto the world upon 2005-02-17 03:51:
jrlen balane said unto the world upon 2005-02-17 02:41:
sir, what seemed to be the problem with this:

SNIP
Hi,
I think the traceback is my fault from an oversight in the code I sent 
you when you posted before. Sorry about that :-[
SNIP
In case that confused anyone, it was only after I sent my reply to the 
list that I noticed the email that I was replying to had been sent to 
me directly. (By reflex I filled in the tutor address thinking I'd hit 
reply rather than reply all.)

I've sent the usual 'better to write to the list' explanation to the OP.
best to all,
Brian vdB
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to read from a txt file

2005-02-17 Thread Brian van den Broek
Brian van den Broek said unto the world upon 2005-02-17 03:51:
 jrlen balane said unto the world upon 2005-02-17 02:41:
sir, what seemed to be the problem with this:
def process(list_of_lines):
data_points = []
for line in list_of_lines:
data_points.append(int(line))
return data_points
data_file = open('C:/Documents and Settings/nyer/Desktop/nyer.txt', 'r')
data = data_file.readline()
print process(data)
SNIP
Traceback (most recent call last):
  File C:\Python23\practices\opentxt, line 12, in -toplevel-
process(data)
  File C:\Python23\practices\opentxt, line 6, in process
data_points.append(int(line))
ValueError: invalid literal for int(): 

The immediate one, due to my advice, is that each line of your file ends 
with a newline character ('\n'). So, you cannot call int on '1000\n'.
Bollocks! Nobody read any thing I write where I am claiming to answer 
anyone!

IDLE 1.1
 int('1000\n')
1000

So, sorry, I don't know what's wrong with the code you sent me, and I 
fear that if I tried to work it out, I'd do more damage. I yield the 
floor as I am off to write Don't post untested code 1000 times.

(I will say I suspect it is the readline vs. readlines, but then 
hopefully no one is reading this ;-)

Sheepishly,
bran vdB
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to read from a txt file

2005-02-17 Thread Danny Yoo


  Traceback (most recent call last):
File C:\Python23\practices\opentxt, line 12, in -toplevel-
  process(data)
File C:\Python23\practices\opentxt, line 6, in process
  data_points.append(int(line))
  ValueError: invalid literal for int():


Hi Brian,

Ah, think about empty lines.

Let's look at the error message again:

ValueError: invalid literal for int():
  ^^^

There's nothing visible there after the colon, and that's our hint.
Notice what happens when we pass int()  some wacky strings:

###
 int(foobar)
Traceback (most recent call last):
  File stdin, line 1, in ?
ValueError: invalid literal for int(): foobar
###

So whatever is being passed to int() should show up in the error message.
This is exactly why getting literal error messages is so wonderful.
*grin*


Since we don't see anything here:

File C:\Python23\practices\opentxt, line 12, in -toplevel-
  process(data)
File C:\Python23\practices\opentxt, line 6, in process
  data_points.append(int(line))
  ValueError: invalid literal for int():


my best guess is that there's an empty line in the file, since we get the
same kind of error if we do this:

###
 int()
Traceback (most recent call last):
  File stdin, line 1, in ?
ValueError: invalid literal for int():
###


Best of wishes to you!


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to read from a txt file

2005-02-17 Thread Gregor Lingl

Brian van den Broek schrieb:
Brian van den Broek said unto the world upon 2005-02-17 03:51:
  jrlen balane said unto the world upon 2005-02-17 02:41:
sir, what seemed to be the problem with this:
def process(list_of_lines):
data_points = []
for line in list_of_lines:
data_points.append(int(line))
return data_points
data_file = open('C:/Documents and Settings/nyer/Desktop/nyer.txt', 'r')
data = data_file.readline()
print process(data)

SNIP
Traceback (most recent call last):
  File C:\Python23\practices\opentxt, line 12, in -toplevel-
process(data)
  File C:\Python23\practices\opentxt, line 6, in process
data_points.append(int(line))
ValueError: invalid literal for int(): 


The immediate one, due to my advice, is that each line of your file 
ends with a newline character ('\n'). So, you cannot call int on 
'1000\n'.

Bollocks! Nobody read any thing I write where I am claiming to answer 
anyone!
Unfortunately I read it. So I'll try a modest advice, too.
If I read invalid literal for int in the error-message,
I try out, what this literal is, by inserting one or two
simple print-statements ;-) :
def process(list_of_lines):
data_points = []
print list_of_lines
for line in list_of_lines:
print (line,)
data_points.append(int(line))
return data_points
Running the program n oe yields:

1000
('1',)
('0',)
('0',)
('0',)
('\n',)
Traceback (most recent call last):
  File C:/_/Tutorstuff/intprog.py, line 12, in -toplevel-
print process(data)
  File C:/_/Tutorstuff/intprog.py, line 6, in process
data_points.append(int(line))
ValueError: invalid literal for int():

which indicates, that your suspicion (readline - readlines)
was right
Regards
Gregor
IDLE 1.1
  int('1000\n')
1000
 
So, sorry, I don't know what's wrong with the code you sent me, and I 
fear that if I tried to work it out, I'd do more damage. I yield the 
floor as I am off to write Don't post untested code 1000 times.

(I will say I suspect it is the readline vs. readlines, but then 
hopefully no one is reading this ;-)

Sheepishly,
bran vdB
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

--
Gregor Lingl
Reisnerstrasse 3/19
A-1030 Wien
Telefon: +43 1 713 33 98
Mobil:   +43 664 140 35 27
Autor von Python für Kids
Website: python4kids.net
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to read from a txt file

2005-02-13 Thread Kent Johnson
Brian van den Broek wrote:
Since you files are quite short, I'd do something like:
code
data_file = open(thedata.txt, 'r') # note -- 'r' not r
data = data_file.readlines()   # returns a list of lines
def process(list_of_lines):
data_points = []
for line in list_of_lines:
data_points.append(int(line))
return data_points
process(data)
This can be done much more simply with a list comprehension using Python's ability to iterate an 
open file directly:
data_file = open('thedata.txt', 'r') # note -- 'thedata.txt' not thedata.txt :-)
data_points = [ int(line) for line in data_file ]

then process the data with something like
for val in data_points:
  # do something with val
  time.sleep(300)
Alternately (and my preference) the processing could be done in the read loop 
like this:
data_file = open('thedata.txt', 'r')
for line in data_file:
  val = int(line)
  # do something with val
  time.sleep(300)
Kent
/code
This assumes that each line of the data file has nothing but a string 
with an int followed by '\n' (for end of line), and that all you need is 
a list of those integers. Maybe these are bad assumptions -- but they 
might get you started.

HTH,
Brian vdB
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to read from a txt file

2005-02-13 Thread Brian van den Broek
Kent Johnson said unto the world upon 2005-02-13 14:04:
Brian van den Broek wrote:
Since you files are quite short, I'd do something like:
code
data_file = open(thedata.txt, 'r') # note -- 'r' not r
data = data_file.readlines()   # returns a list of lines
def process(list_of_lines):
data_points = []
for line in list_of_lines:
data_points.append(int(line))
return data_points
process(data)

This can be done much more simply with a list comprehension using 
Python's ability to iterate an open file directly:
data_file = open('thedata.txt', 'r') # note -- 'thedata.txt' not 
thedata.txt :-)
Gah! :-[   Outsmarting myself in public again. (At least I'm good at 
something :-) )

data_points = [ int(line) for line in data_file ]

then process the data with something like
for val in data_points:
  # do something with val
  time.sleep(300)
Alternately (and my preference) the processing could be done in the read 
loop like this:
data_file = open('thedata.txt', 'r')
for line in data_file:
  val = int(line)
  # do something with val
  time.sleep(300)

Kent
I do get that for the minimal logic I posted, this way is much 
simpler. But, isn't my way with a separate function more easily 
extended? (To deal with cases where there is more than just ints on 
lines, or where the data needs to be similarly processed multiple 
times, etc.)

I do feel a YAGNI coming on, though :-)
Anyway, thanks for improving my attempt to help.
Best,
Brian vdB
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to read from a txt file

2005-02-13 Thread Kent Johnson
Brian van den Broek wrote:
Kent Johnson said unto the world upon 2005-02-13 14:04:
Brian van den Broek wrote:
Since you files are quite short, I'd do something like:
code
data_file = open(thedata.txt, 'r') # note -- 'r' not r
data = data_file.readlines()   # returns a list of lines
def process(list_of_lines):
data_points = []
for line in list_of_lines:
data_points.append(int(line))
return data_points
process(data)

This can be done much more simply with a list comprehension using 
Python's ability to iterate an open file directly:
data_file = open('thedata.txt', 'r') # note -- 'thedata.txt' not 
thedata.txt :-)

Gah! :-[   Outsmarting myself in public again. (At least I'm good at 
something :-) )

data_points = [ int(line) for line in data_file ]

then process the data with something like
for val in data_points:
  # do something with val
  time.sleep(300)
Alternately (and my preference) the processing could be done in the 
read loop like this:
data_file = open('thedata.txt', 'r')
for line in data_file:
  val = int(line)
  # do something with val
  time.sleep(300)

Kent

I do get that for the minimal logic I posted, this way is much simpler. 
But, isn't my way with a separate function more easily extended? (To 
deal with cases where there is more than just ints on lines, or where 
the data needs to be similarly processed multiple times, etc.)
If the processing is per line, any of the three can be extended by calling a user function instead 
of int(), e.g.
def process_line(line):
  # do something with a line
  return val

data_points = [ process_line(line) for line in data_file ]
If you need to maintain some kind of state then the list comprehension breaks down and you might 
want to use
for line in f:
  # ...

or even a class like this:
http://mail.python.org/pipermail/tutor/2005-February/035582.html
If you need to process the list of lines multiple times in different ways then using readlines() is 
appropriate.

I tend to prefer solutions that make fewer intermediate lists, using iterators instead. This seems 
to be the modern Python style with the introduction of list comprehensions, generator functions, 
itertools, generator expressions...

Kent
I do feel a YAGNI coming on, though :-)
Seems appropriate :-)
Kent
Anyway, thanks for improving my attempt to help.
Best,
Brian vdB
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to read from a txt file

2005-02-13 Thread Brian van den Broek
jrlen balane said unto the world upon 2005-02-13 18:45:
ei guys, chill out! 
what if i choose to numbered my data from 1-96 for example. how would
i be able to exclude the numbered part from the data part?

and, mind if I ask, what's a YAGNI by the way?
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor
Hi,
I do hope that my reply to Kent's welcome correction of my suggestions
didn't seem defencive. I'm learning too, and I think a few of us
learners have found that trying to help is also a good way to learn. I
feel confident in trying to do so because Kent and several other
likewise much more experienced people are about to catch my slips. I
may feel a bit embarrassed by them, but I am always grateful to
those how take the time to correct them. So, no defenciveness nor
spirit of competition intended on my part (nor, I am am certain, Kent's).
YAGNI is a slogan of the Extreme and/or Agile programming community.
Stands for You Aren't Going to Need It. The idea is, if you are
thinking of doing something other than (another slogan) `the simplest
thing that could possibly work' -- don't. The rational for
complicating things only when an actual need arises to justify the
complexity is that unnecessary complexity adds maintenance issues evey
bit as much as needed complexity, but without the payoff of additional
functionality. Or, so it seems to a non-XP'er.
Best to all,
Brian vdB
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to read from a txt file

2005-02-13 Thread Kent Johnson
jrlen balane wrote:
and this line:
 data_points.append(int(line))
this would turn the string back to an integer, am i right?
Yes.
and on this one:
data_points = [ int(line) for line in data_file ]
this did not use any read(), is this already equal to readline()? so
this would already store all the data in the txt file to
data_points[], am i right?
Yes. A handy feature of Python is that a file object is iterable - you can use a for loop or list 
comprehension to iterate over the lines of the file without an explicit readline(). So this:
  data_points = [ int(line) for line in data_file ]

is roughly equivalent to this (without the list comprehension):
  data_points = []
  for line in data_file:
data_points.append(int(line))
or this (with explicit readline()):
  data_points = []
  while True:
line = data_file.readline()
if not line:
  break
data_points.append(int(line))
only the list comprehension is much more concise and, when you get used to it, 
much clearer.
thank you guys! ei, you two are not competing, are you? anyway, hope
its a friendly one. for the benifit of all those newbie like me,
hehehe.
Definitely friendly.
Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to read from a txt file

2005-02-13 Thread Alan Gauld
 ei guys, chill out!

Its OK, we often get carried away on flights of fancy here :-)

 what if i choose to numbered my data from 1-96 for example. how
would
 i be able to exclude the numbered part from the data part?

You can use the string split() method to get a list of the
components. Then select the bit you want by indexing the list.

 and, mind if I ask, what's a YAGNI by the way?

Its from the XP programming camp - You Aren't Going to Need It
Basically don't build in fancy features unless you know you need
them, coz most often you won't...

It works up to a point, but if you know you *will* need them
its a lot easier to build the structure up front than to try
to bolt it on later!

Alan G.

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor