<html>
<head>
<style>
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
FONT-SIZE: 10pt;
FONT-FAMILY:Tahoma
}
</style>
</head>
<body class='hmmessage'>
<BR>Hi, Tim,<BR>
 <BR>
Thanks for your help. For some reason, I only get your reply from the forum.<BR>
 <BR>
This is not a homework problem since I have left the school long time ago. I am new to Python and find it very interesting so I decided to try to port a big project from matlab to python. To prove the value of the python, I need to find an python way to do it. Otherwise, the result may be slower than matlab. The input file contains many lines of data starts with a label. The data lengths are not the same and the data type is mixed with int and float. Some lines start with comment sign # need to be removed from the dictionary. The mixed int and float really cause me trouble to convert the data efficiently. I will try your suggestion<BR>
 <BR>
I am really appreciate your help.<BR>
 <BR>
 <BR>
Frank Wang<BR>
 <BR>
 <BR>
<BR>> Date: Tue, 16 Oct 2007 13:52:31 -0500<BR>> From: python.list@tim.thechases.com<BR>> To: fw3@hotmail.co.jp<BR>> CC: python-list@python.org<BR>> Subject: Re: how to convert string to number?<BR>> <BR>> > I have struggling to efficiently convert a string list to<BR>> > number. Here is my problem. I have a file that contains lines<BR>> > such as:<BR>> > <BR>> > data_1 1 1 2 3.5<BR>> > <BR>> > After I read the data from the file by using readlines(), each<BR>> > line contains a string. I use the re moduel to split the line<BR>> > into ['data_1', '1','1','2','3.5']. I want to create a<BR>> > dictionary which contains<BR>> > <BR>> > {'data_1':[1 1 2 3.5]}<BR>> > <BR>> > The problem is I coud not efficiently find a way to convert<BR>> > the string to number.<BR>> > <BR>> > Does anyone know how to create such dictionary efficiently?<BR>> <BR>> Despite my Spidey-sense tingling that this is a homework <BR>> assignment, as similar forms of the question have popped up <BR>> several times in the last week, I supress it this time.<BR>> <BR>> Paraphrasing Andy Dufresne, "Mr. Wang, do you trust your file?"[1]<BR>> <BR>> If you don't trust the content of your file, you have to know either<BR>> <BR>> 1) how many columns of data to expect or<BR>> 2) the type each should be (int or float)<BR>> <BR>> If the same type for each is okay, you can use something like<BR>> <BR>> >>> s = {}<BR>> >>> for line in file('in.txt'):<BR>> ... k,v = line.rstrip('\n').split(None, 1)<BR>> ... s[k] = map(float, v.split())<BR>> ...<BR>> >>> s<BR>> {'data_1': [1.0, 1.0, 2.0, 3.5], 'data_4': [1.0, 1.0, 8.0, 4.5]}<BR>> <BR>> However, if you want them to be the actual types that evaluating <BR>> them would give (thus the trust-your-source issue), you can use this:<BR>> <BR>> >>> s = {}<BR>> >>> for line in file('in.txt'):<BR>> ... k,v = line.rstrip('\n').split(None, 1)<BR>> ... s[k] = map(eval, v.split())<BR>> ...<BR>> >>> s<BR>> {'data_1': [1, 1, 2, 3.5], 'data_4': [1, 1, 8, 4.5]}<BR>> <BR>> <BR>> Both instances don't try to do anything smart with duplicate <BR>> keys, so if you want to append, Bruno Desthuilliers *just* posted <BR>> (in the last hour or so) a nice tip on this using <BR>> setdefault().append()<BR>> <BR>> -tkc<BR>> <BR>> [1]http://www.finestquotes.com/movie_quotes/movie/Shawshank%20Redemption/page/0.htm<BR>> <BR>> <BR>> <BR>> <BR>> <BR><BR><br /><hr />$B!Z(BMSN$B%S%G%*![D65.=E!*6C$-$NBgJ*BPCL$,<B8=!#:n2H(B $BB<>eN6$,OCBj$N$"$N?M$KGw$k(B <a href='http://clk.atdmt.com/GBL/go/msnjpqjl0040000002gbl/direct/01/' target='_new'>http://video.msn.co.jp/rvr/default.htm</a></body>
</html>