[Tutor] how to read from a txt file

Liam Clarke cyresse at gmail.com
Wed Mar 30 11:21:39 CEST 2005


 >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 <nbbalane at gmail.com> 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 <nbbalane at gmail.com> 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 <cyresse at gmail.com> 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 <nbbalane at gmail.com> 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 <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
> > > > >
> > > >
> > >
> > > --
> > > '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.


More information about the Tutor mailing list