Identify and perform actions to data within stated limits

Discussion in 'Python' started by dasacc@gmail.com, Mar 1, 2005.

  1. Guest

    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
     
    , Mar 1, 2005
    #1
    1. Advertising

  2. Guest

    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 : / ...
     
    , Mar 1, 2005
    #2
    1. Advertising

  3. M.E.Farmer Guest

    wrote:
    > 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
     
    M.E.Farmer, Mar 1, 2005
    #3
  4. Steve Holden Guest

    wrote:
    > 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
    --
    Meet the Python developers and your c.l.py favorites March 23-25
    Come to PyCon DC 2005 http://www.pycon.org/
    Steve Holden http://www.holdenweb.com/
    exit
     
    Steve Holden, Mar 1, 2005
    #4
  5. Guest

    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
     
    , Mar 1, 2005
    #5
  6. M.E.Farmer Guest

    Your welcome for the help , be sure to pass it on ;)

    M.E.Farmer
     
    M.E.Farmer, Mar 1, 2005
    #6
  7. Peter Hansen Guest

    wrote:
    > 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
     
    Peter Hansen, Mar 2, 2005
    #7
  8. Mike Meyer Guest

    Peter Hansen <> writes:

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


    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
    --
    Mike Meyer <> http://www.mired.org/home/mwm/
    Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
     
    Mike Meyer, Mar 2, 2005
    #8
  9. Terry Reedy Guest

    <> wrote in message
    news:...
    > 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
     
    Terry Reedy, Mar 2, 2005
    #9
  10. Peter Hansen Guest

    Mike Meyer wrote:
    > Peter Hansen <> writes:
    >>It may look like an array to you, but it's a list. Python
    >>doesn't have arrays

    >
    > 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
     
    Peter Hansen, Mar 3, 2005
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    2,929
    Toby Inkster
    Aug 15, 2004
  2. Nagendra Kumar

    How to stop a scheduler stated using

    Nagendra Kumar, May 18, 2007, in forum: Python
    Replies:
    1
    Views:
    234
    Gabriel Genellina
    May 19, 2007
  3. Peter Bremer
    Replies:
    4
    Views:
    364
  4. Ronald Fischer
    Replies:
    6
    Views:
    149
    Ronald Fischer
    Sep 3, 2007
  5. liz
    Replies:
    4
    Views:
    90
    Randy Webb
    Aug 7, 2004
Loading...

Share This Page