[Tutor] how to read from a txt file

jrlen balane nbbalane at gmail.com
Wed Mar 30 06:56:16 CEST 2005


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 <cyresse at gmail.com> wrote:
> Whoops, golden rule - "Never post untested code"
> Sorry.
> 
> 
> On Mon, 14 Mar 2005 21:05:44 -0500, Kent Johnson <kent37 at tds.net> 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 at 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 at python.org
> http://mail.python.org/mailman/listinfo/tutor
>


More information about the Tutor mailing list