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


Groups > comp.lang.python > #104658 > unrolled thread

Perl to Python again

Started byFillmore <fillmore_remove@hotmail.com>
First post2016-03-11 18:42 -0500
Last post2016-03-12 08:10 -0500
Articles 5 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  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

#104658 — Perl to Python again

FromFillmore <fillmore_remove@hotmail.com>
Date2016-03-11 18:42 -0500
SubjectPerl to Python again
Message-ID<nbvl51$9ch$1@gioia.aioe.org>
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

[toc] | [next] | [standalone]


#104661

Fromsohcahtoa82@gmail.com
Date2016-03-11 16:04 -0800
Message-ID<d77c7379-709f-421f-9d89-3ca56bacd654@googlegroups.com>
In reply to#104658
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.

[toc] | [prev] | [next] | [standalone]


#104662

FromFillmore <fillmore_remove@hotmail.com>
Date2016-03-11 19:15 -0500
Message-ID<nbvn3l$bj0$1@gioia.aioe.org>
In reply to#104658
On 3/11/2016 7:12 PM, Martin A. Brown wrote:
>
> Aside from your csv question today, many of your questions could be
> answered by reading through the manual documenting the standard
> datatypes (note, I am assuming you are using Python 3).
>

are you accusing me of being lazy?

if that's your accusation, then guilty as charged, but

[toc] | [prev] | [next] | [standalone]


#104679

Fromalister <alister.ware@ntlworld.com>
Date2016-03-12 09:40 +0000
Message-ID<puREy.1473053$wX5.1254716@fx40.am4>
In reply to#104662
On Fri, 11 Mar 2016 19:15:48 -0500, Fillmore wrote:

> On 3/11/2016 7:12 PM, Martin A. Brown wrote:
>>
>> Aside from your csv question today, many of your questions could be
>> answered by reading through the manual documenting the standard
>> datatypes (note, I am assuming you are using Python 3).
>>
>>
> are you accusing me of being lazy?
> 
> if that's your accusation, then guilty as charged, but

I not sure if you were being accused of being lazy as such but actually 
being given the suggestion that there are other places that you can find 
these answers that are probably better for a number of reasons

1) Speed, you don't have to wait for someone to reply although i hope you 
are continuing your research whilst waiting

2) Accuracy. I have not seen it here but there are some people who would 
consider it fun to provide an incorrect or dangerous solution to someone 
they though was asking too basic a question

3) Collateral learning, whilst looking for the solution it is highly 
likely that you will unearth other information that answers questions you 
have yet to raise.
 



-- 
Badges?  We don't need no stinking badges.

[toc] | [prev] | [next] | [standalone]


#104700

FromFillmore <fillmore_remove@hotmail.com>
Date2016-03-12 08:10 -0500
Message-ID<nc14ga$1v57$1@gioia.aioe.org>
In reply to#104679
On 03/12/2016 04:40 AM, alister wrote:
> On Fri, 11 Mar 2016 19:15:48 -0500, Fillmore wrote:
> I not sure if you were being accused of being lazy as such but actually
> being given the suggestion that there are other places that you can find
> these answers that are probably better for a number of reasons
>
> 1) Speed, you don't have to wait for someone to reply although i hope you
> are continuing your research whilst waiting
>
> 2) Accuracy. I have not seen it here but there are some people who would
> consider it fun to provide an incorrect or dangerous solution to someone
> they though was asking too basic a question
>
> 3) Collateral learning, whilst looking for the solution it is highly
> likely that you will unearth other information that answers questions you
> have yet to raise.

Alister, you are right, of course. The reality is that I discovered this trove of
a newsgroup and I am rather shamelessly taking advantage of it.
Rest assured that I cross check and learn what is associated with each
and every answer I get. So nothing is wasted. Hopefully your answers
are also useful to others who may find them at a later stage through foofle groups.

Also very important, I am very grateful for the support I am getting from you
and from others.


[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web