What's correct Python syntax?

Chris Angelico rosuav at gmail.com
Tue Jan 14 03:58:17 EST 2014


On Tue, Jan 14, 2014 at 7:46 PM, Igor Korot <ikorot01 at 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



More information about the Python-list mailing list