ok i'm a python newb, but can someone answer the following syntax question that's been bugging me:<br><br>the following line<br><br> temp = [try_int_convert(x) for x in line.strip().split(' ')]<br>
<br>what is temp = [ ...whatever ... ] doing?<br><br>i'm used to [ .. ] as array syntax, clearly i'm missing some capability here. what does python code enclosed in square brackets do? i could guess but seems like i might get a stronger answer from the experts.<br>
<br>~seth<br><br><br><div class="gmail_quote">On Mon, Jun 16, 2008 at 12:08 AM, Al Nevarez <<a href="mailto:anevare2@yahoo.com">anevare2@yahoo.com</a>> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Thanks for the tips everyone.<br>
I'm running an older version of Python.. but the following combination of your suggestions worked just fine in yielding a list of lists with integers and strings preserved.<br>
<br>
my_data[]<br>
data_file='datasource.txt'<br>
<div class="Ih2E3d"><br>
def try_int_convert(sval):<br>
try:<br>
return int(sval)<br>
except ValueError:<br>
return sval<br>
<br>
</div>for line in open(data_file):<br>
temp = [try_int_convert(x) for x in line.strip().split(' ')]<br>
my_data.append(temp)<br>
<br>
<br>
Works fine when the value is blank in the original tab delimited file too (must be a tab there of course). Seems to work perfectly, but does anybody spot any issue?<br>
<br>
<br>
Al<br>
<br>
<br>
<br>
--- On Sun, 6/15/08, Brent Pedersen <<a href="mailto:bpederse@gmail.com">bpederse@gmail.com</a>> wrote:<br>
<br>
> From: Brent Pedersen <<a href="mailto:bpederse@gmail.com">bpederse@gmail.com</a>><br>
> Subject: Re: [Baypiggies] Preserving integers when loading a file into a list<br>
> To: "Jason Culverhouse" <<a href="mailto:jason@mischievous.org">jason@mischievous.org</a>><br>
> Cc: <a href="mailto:baypiggies@python.org">baypiggies@python.org</a><br>
> Date: Sunday, June 15, 2008, 1:46 PM<br>
<div><div></div><div class="Wj3C7c">> On Sun, Jun 15, 2008 at 12:31 PM, Jason Culverhouse<br>
> <<a href="mailto:jason@mischievous.org">jason@mischievous.org</a>> wrote:<br>
> > not x.isdigit() and x or int(x) is going to fail for<br>
> empty string... ''<br>
> > not False and '' or int('')<br>
> <-value error since<br>
> ><br>
> > You could combine Adam's try_int_convert<br>
> ><br>
> > import csv<br>
> > import functools<br>
> > import sys<br>
> ><br>
> > i = functools.partial(map, try_int_convert) # maybe<br>
> convert a list to int<br>
> > t = functools.partial(tuple) # convert to tuple, not<br>
> sure if you need the in<br>
> > tuples<br>
> ><br>
> > #Read a TSV file from stdin and convert<br>
> > [t(i(line)) for line in csv.reader(sys.stdin,<br>
> dialect='excel-tab')]<br>
> ><br>
> > Jason<br>
><br>
> good point. out of curiosity, how is t =<br>
> functools.partial(tuple),<br>
> then using t() different from using tuple() directly?<br>
> _______________________________________________<br>
> Baypiggies mailing list<br>
> <a href="mailto:Baypiggies@python.org">Baypiggies@python.org</a><br>
> To change your subscription options or unsubscribe:<br>
> <a href="http://mail.python.org/mailman/listinfo/baypiggies" target="_blank">http://mail.python.org/mailman/listinfo/baypiggies</a><br>
<br>
<br>
<br>
_______________________________________________<br>
Baypiggies mailing list<br>
<a href="mailto:Baypiggies@python.org">Baypiggies@python.org</a><br>
To change your subscription options or unsubscribe:<br>
<a href="http://mail.python.org/mailman/listinfo/baypiggies" target="_blank">http://mail.python.org/mailman/listinfo/baypiggies</a><br>
</div></div></blockquote></div><br>