What's correct Python syntax?

I

Igor Korot

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

Rustom Mody

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

Igor Korot

Hi, Rustom,

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

Rustom Mody

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

Mark Lawrence

Hi, Rustom,

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.

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

Igor Korot

Hi, Rustom,

Wee you did say:


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

Igor Korot

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


---------- Forwarded message ----------
From: Igor Korot <[email protected]>
Date: Tue, Jan 14, 2014 at 1:50 AM
Subject: Re: What's correct Python syntax?
To: Mark Lawrence <[email protected]>


Hi, Mark,

Hi, Rustom,

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.


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

Rustom Mody

Hi, Rustom,
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[('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
 
I

Igor Korot

Hi, Rustom,

Hi, Rustom,
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[('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
 
R

Rustom Mody

Hi, Rustom,



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
 
P

Peter Otten

Igor said:
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():
('beta', 'alpha', 'delta')
 
N

Ned Batchelder

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

Roy Smith

Igor Korot said:
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:
'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:
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.
 
R

Roy Smith

Igor Korot said:
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:
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}
 
D

Dennis Lee Bieber

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 for i in (1, 3, 0, 2, 4)] ['like', 'dark', 'I', 'my', 'chocolate']
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top