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


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

What's correct Python syntax?

Started byIgor Korot <ikorot01@gmail.com>
First post2014-01-14 00:46 -0800
Last post2014-01-14 08:34 -0500
Articles 17 — 9 participants

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


Contents

  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

#63875 — What's correct Python syntax?

FromIgor Korot <ikorot01@gmail.com>
Date2014-01-14 00:46 -0800
SubjectWhat's correct Python syntax?
Message-ID<mailman.5437.1389689219.18130.python-list@python.org>
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.

[toc] | [next] | [standalone]


#63876

FromRustom Mody <rustompmody@gmail.com>
Date2014-01-14 00:54 -0800
Message-ID<cfd7f2f1-c8cb-4901-a6d7-630674ab20d4@googlegroups.com>
In reply to#63875
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'
>>> 

[toc] | [prev] | [next] | [standalone]


#63878

FromIgor Korot <ikorot01@gmail.com>
Date2014-01-14 01:25 -0800
Message-ID<mailman.5439.1389691509.18130.python-list@python.org>
In reply to#63876
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

[toc] | [prev] | [next] | [standalone]


#63880

FromRustom Mody <rustompmody@gmail.com>
Date2014-01-14 01:37 -0800
Message-ID<41a71031-be78-475f-bab2-822e35f909b7@googlegroups.com>
In reply to#63878
On Tuesday, January 14, 2014 2:55:00 PM UTC+5:30, Igor Korot wrote:
> 
> What if I want field 2 and field 3? ("seq 200" and "length 30")

Wee you did say:

> I'm interesred in only one element, so why should care about everything else? 

So its not clear what you want!

Do you want a one-liner? You could use a regular expression. 
[You will very soon find that the world divides between the regular and the
irregular folks!]

Or you want some other perl-ism? You need to say what...

Or maybe you just want to use scapy instead of tcpdump?

[toc] | [prev] | [next] | [standalone]


#63884

FromIgor Korot <ikorot01@gmail.com>
Date2014-01-14 02:02 -0800
Message-ID<mailman.5443.1389693753.18130.python-list@python.org>
In reply to#63880
Hi, Rustom,

On Tue, Jan 14, 2014 at 1:37 AM, Rustom Mody <rustompmody@gmail.com> wrote:
> On Tuesday, January 14, 2014 2:55:00 PM UTC+5:30, Igor Korot wrote:
>>
>> What if I want field 2 and field 3? ("seq 200" and "length 30")
>
> Wee you did say:
>
>> I'm interesred in only one element, so why should care about everything else?
>
> So its not clear what you want!

Sorry, I thought it would be easier to ask this way. Guess not.

I am actually looking for a way to get a result from split which is
sliced the way I want. Like in my example above.
I mean I can probably make more variable by creating a tuple, but why?
What is the purpose if I want only couple elements out of split.
Doing it Perl way does not help:

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

I can do it this way:

>>> testlist = test.split(',')
>>> print testlist[2]
my

but it will needlessly creates a list on which I will access by the index.

Why? All I need is couple of values out of n-dimensional list (array).

>
> Do you want a one-liner? You could use a regular expression.
> [You will very soon find that the world divides between the regular and the
> irregular folks!]
>
> Or you want some other perl-ism? You need to say what...

Well is there a Python way to do what I want?
I mention Perl only because I'm familiar with the language and this is
easy in it to do that.

Thank you.

>
> Or maybe you just want to use scapy instead of tcpdump?
> --
> https://mail.python.org/mailman/listinfo/python-list

[toc] | [prev] | [next] | [standalone]


#63886

FromRustom Mody <rustompmody@gmail.com>
Date2014-01-14 02:16 -0800
Message-ID<b92f81cf-0701-46cf-9d4f-2b15654b94f7@googlegroups.com>
In reply to#63884
On Tuesday, January 14, 2014 3:32:24 PM UTC+5:30, Igor Korot wrote:
> Hi, Rustom,

> On Tue, Jan 14, 2014 at 1:37 AM, Rustom Mody  wrote:
> > On Tuesday, January 14, 2014 2:55:00 PM UTC+5:30, Igor Korot wrote:
> >> What if I want field 2 and field 3? ("seq 200" and "length 30")
> > Wee you did say:
> >> I'm interesred in only one element, so why should care about everything else?
> > So its not clear what you want!

> Sorry, I thought it would be easier to ask this way. Guess not.

> I am actually looking for a way to get a result from split which is
> sliced the way I want. Like in my example above.
> I mean I can probably make more variable by creating a tuple, but why?
> What is the purpose if I want only couple elements out of split.
> Doing it Perl way does not help:

> 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]

You want this?

>>> test = "I,like,my,chocolate"
>>> test.split(',')
['I', 'like', 'my', 'chocolate']
>>> test.split(',')[2:4]
['my', 'chocolate']


> Well is there a Python way to do what I want? 


Well I for one still dont get what you want!!

Heres a python one-liner using regexps
>>> r=r'(.*) +> +(.*):.*length (\d*)'
>>> re.findall(r,data)
[('192.168.1.6', '192.168.1.7', '30')]

Note: I am NOT suggesting you use regexps. Just that they will do what you want if you are so inclined

[toc] | [prev] | [next] | [standalone]


#63887

FromIgor Korot <ikorot01@gmail.com>
Date2014-01-14 02:35 -0800
Message-ID<mailman.5449.1389695737.18130.python-list@python.org>
In reply to#63886
Hi, Rustom,

On Tue, Jan 14, 2014 at 2:16 AM, Rustom Mody <rustompmody@gmail.com> wrote:
> On Tuesday, January 14, 2014 3:32:24 PM UTC+5:30, Igor Korot wrote:
>> Hi, Rustom,
>
>> On Tue, Jan 14, 2014 at 1:37 AM, Rustom Mody  wrote:
>> > On Tuesday, January 14, 2014 2:55:00 PM UTC+5:30, Igor Korot wrote:
>> >> What if I want field 2 and field 3? ("seq 200" and "length 30")
>> > Wee you did say:
>> >> I'm interesred in only one element, so why should care about everything else?
>> > So its not clear what you want!
>
>> Sorry, I thought it would be easier to ask this way. Guess not.
>
>> I am actually looking for a way to get a result from split which is
>> sliced the way I want. Like in my example above.
>> I mean I can probably make more variable by creating a tuple, but why?
>> What is the purpose if I want only couple elements out of split.
>> Doing it Perl way does not help:
>
>> 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]
>
> You want this?
>
>>>> test = "I,like,my,chocolate"
>>>> test.split(',')
> ['I', 'like', 'my', 'chocolate']
>>>> test.split(',')[2:4]
> ['my', 'chocolate']

Yup, thats it.
Now 2 and 4 - it's a starting point and ending point, right?

Thank you.
>
>
>> Well is there a Python way to do what I want?
>
>
> Well I for one still dont get what you want!!
>
> Heres a python one-liner using regexps
>>>> r=r'(.*) +> +(.*):.*length (\d*)'
>>>> re.findall(r,data)
> [('192.168.1.6', '192.168.1.7', '30')]
>
> Note: I am NOT suggesting you use regexps. Just that they will do what you want if you are so inclined
> --
> https://mail.python.org/mailman/listinfo/python-list

[toc] | [prev] | [next] | [standalone]


#63889

FromRustom Mody <rustompmody@gmail.com>
Date2014-01-14 02:51 -0800
Message-ID<edec3c52-fb70-46ef-b70e-16553320c941@googlegroups.com>
In reply to#63887
On Tuesday, January 14, 2014 4:05:27 PM UTC+5:30, Igor Korot wrote:
> Hi, Rustom,
> 
> 
> 
> On Tue, Jan 14, 2014 at 2:16 AM, Rustom Mody wrote:
> > You want this?
> >
> >>>> test = "I,like,my,chocolate"
> >>>> test.split(',')
> > ['I', 'like', 'my', 'chocolate']
> >>>> test.split(',')[2:4]
> > ['my', 'chocolate']
> 
> 
> Yup, thats it.
> Now 2 and 4 - it's a starting point and ending point, right?

In python ranges are usually lo-inclusive hi-exclusive.
Slices are one case of this

See explanations:
http://docs.python.org/2/tutorial/introduction.html#strings
and
http://stackoverflow.com/questions/509211/pythons-slice-notation

Neat theoretical explanation
http://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EWD831.html

[toc] | [prev] | [next] | [standalone]


#63901

FromRoy Smith <roy@panix.com>
Date2014-01-14 08:47 -0500
Message-ID<roy-78B570.08473514012014@news.panix.com>
In reply to#63884
In article <mailman.5443.1389693753.18130.python-list@python.org>,
 Igor Korot <ikorot01@gmail.com> wrote:

> I can do it this way:
> 
> >>> testlist = test.split(',')
> >>> print testlist[2]
> my
> 
> but it will needlessly creates a list on which I will access by the index.

Stop worrying about needlessly creating lists.  Write the code in a way 
that works and is easy to understand.  If it turns out that it's not 
running fast enough, then you can go back and optimize.

BTW, for those of you into code golf:

>>> line = '192.168.1.6 > 192.168.1.7: ICMP echo request, id 100, seq 
200, length 30'

>>> dict((k,int(v)) for k,v in (s.split() for s in line.split(', ')[1:]))
{'length': 30, 'id': 100, 'seq': 200}

[toc] | [prev] | [next] | [standalone]


#63892

FromPeter Otten <__peter__@web.de>
Date2014-01-14 12:33 +0100
Message-ID<mailman.5451.1389699222.18130.python-list@python.org>
In reply to#63880
Igor Korot wrote:

> I am actually looking for a way to get a result from split which is
> sliced the way I want. Like in my example above.
> I mean I can probably make more variable by creating a tuple, but why?
> What is the purpose if I want only couple elements out of split.
> Doing it Perl way does not help:
> 
> 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
> 
> I can do it this way:
> 
>>>> testlist = test.split(',')
>>>> print testlist[2]
> my
> 
> but it will needlessly creates a list on which I will access by the index.
> 
> Why? All I need is couple of values out of n-dimensional list (array).

Python has no dedicated syntax for picking arbitrary items from a list 
If you are only concerned about printing use format():

>>> items = ["alpha", "beta", "gamma", "delta"]
>>> print "{1} {3} {0}".format(*items)
beta delta alpha

If you want to work with the values use operator.itemgetter():

>>> from operator import itemgetter
>>> itemgetter(1, 0, -1)(items)
('beta', 'alpha', 'delta')


[toc] | [prev] | [next] | [standalone]


#63893

FromNed Batchelder <ned@nedbatchelder.com>
Date2014-01-14 07:19 -0500
Message-ID<mailman.5452.1389701993.18130.python-list@python.org>
In reply to#63880
On 1/14/14 6:33 AM, Peter Otten wrote:
> Python has no dedicated syntax for picking arbitrary items from a list
> If you are only concerned about printing use format():
>
>>>> >>>items = ["alpha", "beta", "gamma", "delta"]
>>>> >>>print "{1} {3} {0}".format(*items)
> beta delta alpha

.format also supports item access directly:

 >>> items = ["alpha", "beta", "gamma", "delta"]
 >>> print "{0[1]} {0[3]} {0[0]}".format(items)
beta delta alpha

It's clunkier in this example, but if you have more than one value being 
formatted, this (and the "{0.foo}" syntax) can make digging into nested 
data more convenient.

-- 
Ned Batchelder, http://nedbatchelder.com

[toc] | [prev] | [next] | [standalone]


#63904

FromDennis Lee Bieber <wlfraed@ix.netcom.com>
Date2014-01-14 08:58 -0500
Message-ID<mailman.5457.1389707915.18130.python-list@python.org>
In reply to#63880
On Tue, 14 Jan 2014 02:02:24 -0800, Igor Korot <ikorot01@gmail.com>
declaimed the following:

>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]

	So close, as long as you want a contiguous range of parts.

>>> "I,like,my,chocolate".split(",")[2:3]
['my']
>>> "I,like,my,chocolate".split(",")[2:4]
['my', 'chocolate']
>>> "I,like,my,chocolate".split(",")[1:-1]
['like', 'my']
>>> 

	For arbitrary items, as long as you don't ask for something higher than
in the split..

>>> splt = "I,like,my,dark,chocolate".split(",")
>>> splt
['I', 'like', 'my', 'dark', 'chocolate']
>>> [splt[i] for i in (1, 3, 0, 2, 4)]
['like', 'dark', 'I', 'my', 'chocolate']
>>> 
-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
    wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

[toc] | [prev] | [next] | [standalone]


#63882

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2014-01-14 09:37 +0000
Message-ID<mailman.5442.1389692261.18130.python-list@python.org>
In reply to#63876
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.

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

Mark Lawrence

[toc] | [prev] | [next] | [standalone]


#63885

FromIgor Korot <ikorot01@gmail.com>
Date2014-01-14 02:03 -0800
Message-ID<mailman.5444.1389693790.18130.python-list@python.org>
In reply to#63876
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

[toc] | [prev] | [next] | [standalone]


#63963

FromLarry Hudson <orgnut@yahoo.com>
Date2014-01-14 22:00 -0800
Message-ID<xf-dnbdBioifuUvPnZ2dnUVZ_jmdnZ2d@giganews.com>
In reply to#63885
On 01/14/2014 02:03 AM, Igor Korot wrote:
[snip]

> 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
>

Try again...  but use a colon not a comma, [2:3]

Two additional comments:
(1)  test.split(',')[2:3] will give you ["my"] only.  The slicing syntax starts with the first 
index and goes up to but NOT INCLUDING the second.  In this case it is the same as the single 
index, [2].  You want either [2:4] or [2:], or even [2:500].  Any value >= the length of the 
list (or whatever sequence) is acceptable as the ending index in a slice.  It's probably not a 
good idea to use a value like this, but it does work.  And obviously, don't try to read with an 
out-of-bounds index, but it does work as the _ending_ index in a slice.

(2)  A comma-separated list of data items IS a tuple, even without the usual enclosing 
parenthesis.  That is your error here -- [2,3] is the same as [(2,3)], which is a tuple.

      -=- Larry -=-

[toc] | [prev] | [next] | [standalone]


#63890

FromAlister <alister.ware@ntlworld.com>
Date2014-01-14 10:59 +0000
Message-ID<AM8Bu.26289$Ke3.20337@fx35.am4>
In reply to#63875
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"

[toc] | [prev] | [next] | [standalone]


#63899

FromRoy Smith <roy@panix.com>
Date2014-01-14 08:34 -0500
Message-ID<roy-BA6517.08343114012014@news.panix.com>
In reply to#63875
In article <mailman.5437.1389689219.18130.python-list@python.org>,
 Igor Korot <ikorot01@gmail.com> 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.

One possibility would be to forget about all the punctuation and just 
use "length " (note the trailing space) as the split delimiter:

>>> line = '192.168.1.6 > 192.168.1.7: ICMP echo request, id 100, seq 
200, length 30'
>>> line.split('length ')
'30'

this will only work if you're sure that "length " can never appear 
anywhere else in the line.  Another, perhaps more idiomatic, way would 
be:

>>> _, length = line.split('length ')
>>> print length
30

What's happening here is split() is returning a list of two items, which 
you then unpack into two variables, "_" and "length".  It's common to 
unpack unwanted fields into "_", as a hint (to the reader) that it's 
unused.

[toc] | [prev] | [standalone]


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


csiph-web