473,893 Members | 1,794 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Identify and perform actions to data within stated limits

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

Jul 18 '05 #1
9 1346
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 : / ...

Jul 18 '05 #2
da****@gmail.co m 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

Jul 18 '05 #3
da****@gmail.co m 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
Jul 18 '05 #4
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

Jul 18 '05 #5
Your welcome for the help , be sure to pass it on ;)

M.E.Farmer

Jul 18 '05 #6
da****@gmail.co m 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
Jul 18 '05 #7
Peter Hansen <pe***@engcorp. com> writes:
da****@gmail.co m 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 <mw*@mired.or g> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Jul 18 '05 #8

<da****@gmail.c om> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.com.. .
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

Jul 18 '05 #9
Mike Meyer wrote:
Peter Hansen <pe***@engcorp. com> 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
Jul 18 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
3074
by: Brad H McCollum | last post by:
I'm writing an application using VB 6.0 as the front-end GUI, and the MSDE version of SQL Server as the back-end (it's a program for a really small # of users --- less then 3-4). I'm trying to determine, through the Visual Basic interface, the permissions of each user that's using the application on his/her machine. For example, let's say I'm user "Michael" that's sitting down at my machine using the app. I've written. The security...
8
487
by: DraguVaso | last post by:
Hi, I want my application do different actions depending on the exception it gets. For exemple: I have an SQL-table with a unique index. In case I try to Insert a record that's alreaddy in it I get this exception: "Cannot insert duplicate key row in object 'tblTelephones' with unique index 'UniqueValues'." What I'm looking for is a way to identify the exception: in case I get this
1
2305
by: ammarton | last post by:
Hello all...I'm a bit new to working with Macros in Access so forgive me if the terminology I use is not accurate. To preface this, basically I am using a form on a replicated database so the end-user can filter on a specific report they want to see. This database was designed by my predecessor (of which he left no documentation) and I need to add some additional functions to this end-user filter report. Within one Macro, he has over...
1
1793
by: Dennis | last post by:
If a Window's form contains two textboxes, txtUserName for names and the other txtDimension for mathematical values, how could the program code be set up to identify that only letter character(s) are entered into txtUserName .... and that only numbers and decimals are entered into txtDimension ? Secondly, how could the program code be set up to inform the user that the wrong type of data was entered ? All constructive suggestions are...
5
2378
by: Ariel Gimenez | last post by:
Morning! After several hours breaking my mind, finally my code works, but i think is trash-code, can someone tellme how is the correct way to access the value of my cells within a datagrid?... in some cases i can access the values with e.Item.Cells.Text, but in other cases i have to do weird thinks like the following: TextBox pp; //I know...disgusting!!! pp= (TextBox) e.Item.Cells.Controls ; int id = Convert.ToInt32(pp.Text );
4
6695
by: loretta | last post by:
I have data within an xml tag that is being truncated when being read into a javascript variable. On Firefox, I am only getting up to 4096 characters. On IE, I am getting 31324 characters. I can view the xml source, all the data is there. I am using javascript function getElementsByTagName to read the data from the tag and then the firstChild and nodeValue notations to get the data. I can't find any references to xml size limits, but I am...
9
1960
by: Ben R. | last post by:
Hi guys, I've got a DB table of timecards with these fields in the table: ID (Int) UserID (Int) DateWorked (DateTime) HoursWorkedOnThatDate (Double) I'd like to display a grid, with Monday - Sunday across the top (in columns)
4
5004
by: wrldruler | last post by:
Hello, First, I know it's against "Access Law" to save calculations in a table, but....I want/need to. I currently have sub-totals being calculated inside a form, using DMax, DCount, and DSum. But these aggregate functions are slowing down the interface and annoying me. So I have decided not to show sub- totals on the form.
1
2930
by: pushrodengine via AccessMonster.com | last post by:
Is there a way to log user actions? What I would like is to be able to log user activities within the database. The table “tblUserEvents” would contain two fields. Field one “EventTime” would be a date/time stamp of when the action was performed. Field two “EventDescription” would be a description of the action performed. Example 1: the user presses the Add button. The date/time is inserted into “EventTime” and the...
0
9985
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
11244
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10839
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10469
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
8022
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7173
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5858
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6066
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3289
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.