[Tutor] multiple assignments when reading a file

Amit Saha amitsaha.in at gmail.com
Thu Jul 11 05:28:54 CEST 2013


On Thu, Jul 11, 2013 at 1:24 PM, Sivaram Neelakantan
<nsivaram.net at gmail.com> wrote:
>
> I'm aware of
>
> var1, var2  = lines.split()
>
> kind of assignments
>
> How do I get to do
>
> x1..xn = lines.split()
>
> instead of typing out n var names?  Is there some pythonic way to map
> all the data fields automagically given a starter var?
>
> I have dataset that's got 100s of fields and I really don't want to
> type them out.

The split() method returns you a list, so you could do something like:

>>> line="abra ca dabra"
>>> words = line.split()

The words will now be in the list, words. You could then enumerate the
list to access the individual words.



>
>  sivaram
>  --
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor



--
http://echorand.me


More information about the Tutor mailing list