What's correct Python syntax?

Mark Lawrence breamoreboy at yahoo.co.uk
Tue Jan 14 04:37:48 EST 2014


On 14/01/2014 09:25, Igor Korot wrote:
> Hi, Rustom,
>
> On Tue, Jan 14, 2014 at 12:54 AM, Rustom Mody <rustompmody at gmail.com> wrote:
>> On Tuesday, January 14, 2014 2:16:56 PM UTC+5:30, 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. ;-)
>>
>>
>>>>> data="192.168.1.6 > 192.168.1.7: ICMP echo request, id 100, seq 200, length 30"
>>>>> (add,traff) = data.split(':')
>>>>> add
>> '192.168.1.6 > 192.168.1.7'
>>>>> traff
>> ' ICMP echo request, id 100, seq 200, length 30'
>>>>> lenn = traff.split(',')
>>>>> lenn = traff.split(',')[3]
>>>>> lenn
>> ' length 30'
>
> What if I want field 2 and field 3? ("seq 200" and "length 30")
>
> Thank you.
>
>>>>>
>> --
>> https://mail.python.org/mailman/listinfo/python-list

Please do a little work before asking such a trivial question, it's 
hardly difficult from the interactive interpreter, particularly when you 
already have an example to start with.

-- 
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.

Mark Lawrence




More information about the Python-list mailing list