Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #104661
| X-Received | by 10.129.39.146 with SMTP id n140mr8956145ywn.22.1457741098710; Fri, 11 Mar 2016 16:04:58 -0800 (PST) |
|---|---|
| X-Received | by 10.50.61.200 with SMTP id s8mr122611igr.3.1457741098675; Fri, 11 Mar 2016 16:04:58 -0800 (PST) |
| Path | csiph.com!usenet.blueworldhosting.com!feeder01.blueworldhosting.com!peer01.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!y89no6890189qge.0!news-out.google.com!k1ni15853igd.0!nntp.google.com!av4no164284igc.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail |
| Newsgroups | comp.lang.python |
| Date | Fri, 11 Mar 2016 16:04:58 -0800 (PST) |
| In-Reply-To | <nbvl51$9ch$1@gioia.aioe.org> |
| Complaints-To | groups-abuse@google.com |
| Injection-Info | glegroupsg2000goo.googlegroups.com; posting-host=15.65.244.13; posting-account=LpZxbAkAAADmdg1egflF6w8zbbyJPDNY |
| NNTP-Posting-Host | 15.65.244.13 |
| References | <nbvl51$9ch$1@gioia.aioe.org> |
| User-Agent | G2/1.0 |
| MIME-Version | 1.0 |
| Message-ID | <d77c7379-709f-421f-9d89-3ca56bacd654@googlegroups.com> (permalink) |
| Subject | Re: Perl to Python again |
| From | sohcahtoa82@gmail.com |
| Injection-Date | Sat, 12 Mar 2016 00:04:58 +0000 |
| Content-Type | text/plain; charset=ISO-8859-1 |
| X-Received-Bytes | 1824 |
| X-Received-Body-CRC | 1956242275 |
| Xref | csiph.com comp.lang.python:104661 |
Show key headers only | 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 | Next — Previous in thread | Next in thread | Find similar | Unroll 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