What's correct Python syntax?
Alister
alister.ware at ntlworld.com
Tue Jan 14 05:59:44 EST 2014
On Tue, 14 Jan 2014 00:46:56 -0800, Igor Korot wrote:
> Hi, ALL,
> I'm trying to process a file which has following lines:
>
> 192.168.1.6 > 192.168.1.7: ICMP echo request, id 100, seq 200, length 30
>
> (this is the text file out of tcpdump)
>
> Now I can esily split the line twice: once by ':' symbol to separate
> address and the protocol information and the second time by ',' to get
> information about the protocol.
> However, I don't need all the protocol info. All I'm interested in is
> the last field, which is length.
>
> Is there a way to write something like this:
>
> for data in f:
> (address,traffic) = string.split(data, ':')
> length = string.split(traffic, ',')[3]
>
> I'm interesred in only one element, so why should care about everything
> else?
> This can be easily done in Perl, but I'm stuck with Python now. ;-)
>
> Thank you.
Am I missing something obvious here?
just split on ','
field [0] will contain a mix of data but who cares? you don't want it
anyway (you can always process it again afterwards.
>>> a='192.168.1.6 > 192.168.1.7: ICMP echo request, id 100, seq 200,
length 30'
>>> data=a.split(',')
>>> data
['192.168.1.6 > 192.168.1.7: ICMP echo request', ' id 100', ' seq 200', '
length 30']
>>> data[3]
' length 30'
--
It's not against any religion to want to dispose of a pigeon.
-- Tom Lehrer, "Poisoning Pigeons in the Park"
More information about the Python-list
mailing list