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


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

Re: What's correct Python syntax?

Started byChris Angelico <rosuav@gmail.com>
First post2014-01-14 19:58 +1100
Last post2014-01-14 11:43 +0200
Articles 2 — 2 participants

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

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: What's correct Python syntax? Chris Angelico <rosuav@gmail.com> - 2014-01-14 19:58 +1100
    Re: What's correct Python syntax? Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2014-01-14 11:43 +0200

#63877 — Re: What's correct Python syntax?

FromChris Angelico <rosuav@gmail.com>
Date2014-01-14 19:58 +1100
SubjectRe: What's correct Python syntax?
Message-ID<mailman.5438.1389690263.18130.python-list@python.org>
On Tue, Jan 14, 2014 at 7:46 PM, Igor Korot <ikorot01@gmail.com> wrote:
> 192.168.1.6 > 192.168.1.7: ICMP echo request, id 100, seq 200, length 30
>
> However, I don't need all the protocol info. All I'm interested in is
> the last field, which is length.

You can split on any string. If you're confident that this is the only
instance of the word "length", you can split on that:

for data in f:
    # This will throw an exception if there's no " length "
    # or if there are two of them. This means you're safe;
    #  if anything unexpected happens, you'll know.
    _, length = data.split(" length ")
    # process length

Alternatively, you can split on the space and take just the very last word:

for data in f:
    length = data.split(" ")[-1]
    # process length

Either way, the length will be a string. If you need it as an integer,
just do this:

    length = int(length)

>From there, you can do whatever analysis you need.

Hope that helps!

ChrisA

[toc] | [next] | [standalone]


#63883

FromJussi Piitulainen <jpiitula@ling.helsinki.fi>
Date2014-01-14 11:43 +0200
Message-ID<qot4n567vaj.fsf@ruuvi.it.helsinki.fi>
In reply to#63877
Chris Angelico writes:

> Alternatively, you can split on the space and take just the very
> last word:
> 
> for data in f:
>     length = data.split(" ")[-1]
>     # process length

Also, data.rsplit(' ', 1) will split data in two at the last space.

help(str.rsplit)

[toc] | [prev] | [standalone]


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


csiph-web