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


Groups > comp.lang.python > #63885

Fwd: What's correct Python syntax?

References <mailman.5437.1389689219.18130.python-list@python.org> <cfd7f2f1-c8cb-4901-a6d7-630674ab20d4@googlegroups.com> <CA+FnnTzv_uz+V8SLK7_VfPvpiDMEgLyA5NndM3eNjftPCe+s5w@mail.gmail.com> <lb30gb$h8m$1@ger.gmane.org> <CA+FnnTwHGUeQSfPN2699v5ZH6rZUj_MNY8MH5GmLG3pLRup1Sg@mail.gmail.com>
Date 2014-01-14 02:03 -0800
Subject Fwd: What's correct Python syntax?
From Igor Korot <ikorot01@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.5444.1389693790.18130.python-list@python.org> (permalink)

Show all headers | View raw


Sorry, that was sent to Mark directly.
Resending to the list.


---------- Forwarded message ----------
From: Igor Korot <ikorot01@gmail.com>
Date: Tue, Jan 14, 2014 at 1:50 AM
Subject: Re: What's correct Python syntax?
To: Mark Lawrence <breamoreboy@yahoo.co.uk>


Hi, Mark,

On Tue, Jan 14, 2014 at 1:37 AM, Mark Lawrence <breamoreboy@yahoo.co.uk> wrote:
> On 14/01/2014 09:25, Igor Korot wrote:
>>
>> Hi, Rustom,
>>
>> On Tue, Jan 14, 2014 at 12:54 AM, Rustom Mody <rustompmody@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.

C:\Documents and Settings\Igor.FORDANWORK\Desktop\winpdb>python
Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> test = "I,like,my,chocolate"
>>> print test.split(',')[2,3]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: list indices must be integers, not tuple

Like I said, I'm more used to Perl, but need to work with Python for a moment.

Thank you.

>
> --
> My fellow Pythonistas, ask not what our language can do for you, ask what
> you can do for our language.
>
> Mark Lawrence
>
> --
> https://mail.python.org/mailman/listinfo/python-list

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

What's correct Python syntax? Igor Korot <ikorot01@gmail.com> - 2014-01-14 00:46 -0800
  Re: What's correct Python syntax? Rustom Mody <rustompmody@gmail.com> - 2014-01-14 00:54 -0800
    Re: What's correct Python syntax? Igor Korot <ikorot01@gmail.com> - 2014-01-14 01:25 -0800
      Re: What's correct Python syntax? Rustom Mody <rustompmody@gmail.com> - 2014-01-14 01:37 -0800
        Re: What's correct Python syntax? Igor Korot <ikorot01@gmail.com> - 2014-01-14 02:02 -0800
          Re: What's correct Python syntax? Rustom Mody <rustompmody@gmail.com> - 2014-01-14 02:16 -0800
            Re: What's correct Python syntax? Igor Korot <ikorot01@gmail.com> - 2014-01-14 02:35 -0800
              Re: What's correct Python syntax? Rustom Mody <rustompmody@gmail.com> - 2014-01-14 02:51 -0800
          Re: What's correct Python syntax? Roy Smith <roy@panix.com> - 2014-01-14 08:47 -0500
        Re: What's correct Python syntax? Peter Otten <__peter__@web.de> - 2014-01-14 12:33 +0100
        Re: What's correct Python syntax? Ned Batchelder <ned@nedbatchelder.com> - 2014-01-14 07:19 -0500
        Re: What's correct Python syntax? Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2014-01-14 08:58 -0500
    Re: What's correct Python syntax? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-01-14 09:37 +0000
    Fwd: What's correct Python syntax? Igor Korot <ikorot01@gmail.com> - 2014-01-14 02:03 -0800
      Re: Fwd: What's correct Python syntax? Larry Hudson <orgnut@yahoo.com> - 2014-01-14 22:00 -0800
  Re: What's correct Python syntax? Alister <alister.ware@ntlworld.com> - 2014-01-14 10:59 +0000
  Re: What's correct Python syntax? Roy Smith <roy@panix.com> - 2014-01-14 08:34 -0500

csiph-web