[Tutor] assigning types(?) to list elements
François Granger
franois.granger@free.fr
Mon, 14 Oct 2002 15:24:53 +0200
on 14/10/02 14:08, Joseph Paish at jpaish@freenet.edmonton.ab.ca wrote:
> file_handle = open ('/path/to/datafile.txt', 'r')
>
> for each_line in file_handle.readlines() :
> each_line = each_line.rstrip() # strip off carriage return
> temp1, temp2, temp3, temp4, temp5, temp6 = string.split(each_line)
> temp2 = float(temp2)
> temp3 = int(temp3)
> temp4 = int(temp4)
> temp5 = float(temp5)
> temp6 = float(temp6)
This seems to do what you want:
typeslist = [str,float,int,int,float,float]
test = 'abc 123.45 74 98 234.56 345.67'
temp = test.split()
# a sample record is : 'abc 123.45 74 98 234.56 345.67'
result = []
for i in range(len(temp)):
result.append(typeslist[i](temp[i]))
pass
--
Le courrier est un moyen de communication. Les gens devraient
se poser des questions sur les implications politiques des choix (ou non
choix) de leurs outils et technologies. Pour des courriers propres :
<http://marc.herbert.free.fr/mail/> -- <http://minilien.com/?IXZneLoID0>