Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #104661

Re: Perl to Python again

Newsgroups comp.lang.python
Date 2016-03-11 16:04 -0800
References <nbvl51$9ch$1@gioia.aioe.org>
Message-ID <d77c7379-709f-421f-9d89-3ca56bacd654@googlegroups.com> (permalink)
Subject Re: Perl to Python again
From sohcahtoa82@gmail.com

Show all headers | View raw


On Friday, March 11, 2016 at 3:42:36 PM UTC-8, Fillmore wrote:
> So, now I need to split a string in a way that the first element goes 
> into a string and the others in a list:
> 
> while($line = <STDIN>) {
> 
>      my ($s,@values)  = split /\t/,$line;
> 
> I am trying with:
> 
> for line in sys.stdin:
>      s,values = line.strip().split("\t")
>      print(s)
> 
> but no luck:
> 
> ValueError: too many values to unpack (expected 2)
> 
> What's the elegant python way to achieve this?
> 
> Thanks

I'd call the split, assign it to a temp value, then pull from there, like this:

parts = line.strip().split('\t')
s = parts[0]
values = parts[1:]

There might be a one-line way to do it, but I can't think of one that doesn't rely on calling split() twice.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Perl to Python again Fillmore <fillmore_remove@hotmail.com> - 2016-03-11 18:42 -0500
  Re: Perl to Python again sohcahtoa82@gmail.com - 2016-03-11 16:04 -0800
  Re: Perl to Python again Fillmore <fillmore_remove@hotmail.com> - 2016-03-11 19:15 -0500
    Re: Perl to Python again alister <alister.ware@ntlworld.com> - 2016-03-12 09:40 +0000
      Re: Perl to Python again Fillmore <fillmore_remove@hotmail.com> - 2016-03-12 08:10 -0500

csiph-web