Identify and perform actions to data within stated limits

D

dasacc

Hi, I've only been using python for two days now but I'm working on it.
I have the following text:

<select><option></option><option></option></select><select><option></option></select>

My question is how can I specify to only work with the first instance
of <select>...</select> via any sort of substitute. If that isn't
possible this is what I was doing with bash script

I seperated the items as follows
<select>
<option></option>
<option></option></select>
<select>
<option></option><select>

Select has a 'name' value and i want to substitute <option> with that
name value. In bash I grep'd for the first instance of <option> and
used awk to grab the line number, subtracted the line number by one
which gave me the select are with the name value. The select had
already been parsed so that the 'name' value was first. I then assigned
that 'name' value to a variable and did a substitue via sed on the
specified line number and looped this until my line number minus 1
equaled -1.

I've been trying to figure out how to do something similar in python. I
can get my variable set, but I'm trying to figure out how to target the
first instance of whatever I specify.

I was also thinking that if I have it split up like
<select><option></option><option></option></select>
That I could specify a substitution on just that line. This would be
the best scenerio for my feeble mind to comprehend I believe.

Can anyone help me (or point me to helpful documentation) to identify
line numbers and perform substitutions on only the specified line
number or something similar?

Thanks,
Daniel
 
D

dasacc

I found out that doing a re.findall will split up the results into an
array, or rather something called a list (looks like an array to me).
I'd be set if i could just count the elements in the array but I can't
seem to find anything in the documentation on how to : / ...
 
M

M.E.Farmer

I found out that doing a re.findall will split up the results into an
array, or rather something called a list (looks like an array to me).
I'd be set if i could just count the elements in the array but I can't
seem to find anything in the documentation on how to : / ...
Hello,
List have methods as do most other built-in objects in Python.
If you want to know what methods an object has just dir() it.
Py> q = [1,2,3,4,5,6,7,8]# create a list
Py> dir(q)
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__',
'__delslice__', '__doc__', '__eq__', '__ge__', '__getattribute__',
'__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__',
'__imul__', '__init__', '__le__', '__len__', '__lt__', '__mul__',
'__ne__', '__new__', '__reduce__', '__repr__', '__rmul__',
'__setattr__', '__setitem__', '__setslice__', '__str__', 'append',
'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse',
'sort']
The exact output depends on your Python version.
Notice that there are methods called count and index.
Open an interpreter and try playing with them and see what they do.
If you weant to know what an object or methods docs say, use help().
Py> help(q.count)
Help on built-in function count:

count(...)
L.count(value) -> integer -- return number of occurrences of value

If you want to know the number of items in a list ( or most builtins )
use len().
Py> len(q)
8
Spend time reading the docs. It will save you months of work.
Also learn the library it has many modules that will simplify your code
and save your mind.
http://www.python.org/doc/
hth,
M.E.Farmer
 
S

Steve Holden

I found out that doing a re.findall will split up the results into an
array, or rather something called a list (looks like an array to me).
I'd be set if i could just count the elements in the array but I can't
seem to find anything in the documentation on how to : / ...

$ python
Python 2.4 (#1, Dec 4 2004, 20:10:33)
[GCC 3.3.3 (cygwin special)] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
>>> l = [0, 1, 'two']
>>> len(l) 3
>>> l[2] 'two'
>>>

Does this help at all?

regards
Steve
 
D

dasacc

dir(*) !!! That's beautiful! I was just wanting to know what was
available to an object. I was thinking, if there was just something
that quickly told me that info I could look through the documentation
quicker :D

I found the len(*) obscurely mentioned on someones webpage. Thanks for
the dir(*) pointer though! That will help me greatly
 
P

Peter Hansen

I found out that doing a re.findall will split up the results into an
array, or rather something called a list (looks like an array to me).

It may look like an array to you, but it's a list. Python
doesn't have arrays, unless you count something like the
numarray/Numeric extension module. (You probably just meant
that Python's list looks like the sort of object you've known
as an "array" in other languages. If that's the case,
fine, but call it a list when you're working with Python
to avoid confusion.)
I'd be set if i could just count the elements in the array but I can't
seem to find anything in the documentation on how to : / ...

somelist = [1, 2, 3, 4]
print len(somelist)

This will print "4", which is a count of the elements in the list.
Is that what you wanted? If not, please clarify.

-Peter
 
M

Mike Meyer

Peter Hansen said:
It may look like an array to you, but it's a list. Python
doesn't have arrays

Huh?

guru% pydoc array
Help on module array:

NAME
array

FILE
/usr/opt/lib/python2.4/lib-dynload/array.so

MODULE DOCS
http://www.python.org/doc/current/lib/module-array.html

DESCRIPTION
This module defines an object type which can efficiently represent
an array of basic values: characters, integers, floating point
numbers. Arrays are sequence types and behave very much like lists,
except that the type of objects stored in them is constrained. The
type is specified at object creation time by using a type code, which
is a single character. The following type codes are defined:

etc.

<mike
 
T

Terry Reedy

I found the len(*) obscurely mentioned on someones webpage.

*All* the built functions are prominently listed and described in Library
Reference 2.1Built-in Functions. I urge you and any beginner to at least
read the whole of chapter 2.

Terry J. Reedy
 
P

Peter Hansen

Mike said:
Huh?

guru% pydoc array
....

You got me :), although you did prune a somewhat relevant
part of my above comment, which continued "unless you
count something like the numarray/Numeric extension module."

Although I admit I _had_ forgotten about the standard
array module, I could just as well have continued
still further with "or the standard array module" and
it would have been roughly the same point. To wit,
a Python list is not an "array", it just acts a lot
like what other languages call arrays and you'll get into
trouble referring to it as such. Call it a list and
all of us will understand. Call it an array and we'll
have at least three possibilities...

But thanks for the correction nonetheless!

(I think I've used that module once, so perhaps I can
be forgiven the oversight. :) )

-Peter
 

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,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top