473,408 Members | 2,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

re.search - just skip it

Input is this:

SET1_S_W CHAR(1) NOT NULL,
SET2_S_W CHAR(1) NOT NULL,
SET3_S_W CHAR(1) NOT NULL,
SET4_S_W CHAR(1) NOT NULL,
;

..py says:

import re, string, sys
s_ora = re.compile('.*S_W.*')
lines = open("y.sql").readlines()
for i in range(len(lines)):
try:
if s_ora.search(lines[i]): del lines[i]
except IndexError:
open("z.sql","w").writelines(lines)

but output is:

SET2_S_W CHAR(1) NOT NULL,
SET4_S_W CHAR(1) NOT NULL,
;

It should delete every, not every other!

thx,

RasDJ

Jul 18 '05 #1
3 1626
wrote:
Input is this:

SET1_S_W CHAR(1) NOT NULL,
SET2_S_W CHAR(1) NOT NULL,
SET3_S_W CHAR(1) NOT NULL,
SET4_S_W CHAR(1) NOT NULL,
;

.py says:

import re, string, sys
s_ora = re.compile('.*S_W.*')
lines = open("y.sql").readlines()
for i in range(len(lines)):
try:
if s_ora.search(lines[i]): del lines[i]
except IndexError:
open("z.sql","w").writelines(lines)

but output is:

SET2_S_W CHAR(1) NOT NULL,
SET4_S_W CHAR(1) NOT NULL,
;

It should delete every, not every other!


No, it should delete every other line since that is what happens if you use
an index to iterate over a list while deleting items from the same list.
Whenever you delete an item the following items shuffle down and then you
increment the loop counter which skips over the next item.

The fact that you got an IndexError should have been some sort of clue that
your code was going to go wrong.

Try one of these:
iterate backwards
iterate over a copy of the list but delete from the original
build a new list containing only those lines you want to keep

also, the regex isn't needed here, and you should always close files when
finished with them.

Something like this should work (untested):

s_ora = 'S_W'
input = open("y.sql")
try:
lines = [ line for line in input if s_ora in line ]
finally:
input.close()

output = open("z.sql","w")
try:
output.write(str.join('', lines))
finally:
output.close()
Jul 18 '05 #2
<ra***@frontiernet.net> wrote:
but output is:

SET2_S_W CHAR(1) NOT NULL,
SET4_S_W CHAR(1) NOT NULL,

It should delete every, not every other!


for i in range(len(lines)):
try:
if s_ora.search(lines[i]): del lines[i]
except IndexError:
...

when you loop over a range, the loop counter is incremented also if you delete
items. but when you delete items, the item numbering changes, so you end up
skipping over an item every time the RE matches.

to get rid of all lines for which s_ora.search matches, try this

lines = [line for line in lines if not s_ora.search(line)]

for better performance, get rid of the leading and trailing ".*" parts of your
pattern, btw. not that it matters much in this case (unless the SQL state-
ment is really huge).

</F>

Jul 18 '05 #3
ra***@frontiernet.net wrote:
Input is this:

SET1_S_W CHAR(1) NOT NULL,
SET2_S_W CHAR(1) NOT NULL,
SET3_S_W CHAR(1) NOT NULL,
SET4_S_W CHAR(1) NOT NULL,
;

.py says:

import re, string, sys
s_ora = re.compile('.*S_W.*')
lines = open("y.sql").readlines()
for i in range(len(lines)):
try:
if s_ora.search(lines[i]): del lines[i]
When you delete for example lines[0], the indices of the following lines change. So the former
lines[1] is now lines[0] and will not be checked.

The simplest way to do this is with a list comprehension:
lines = [ line for line in lines if not s_ora.search(line) ]

Even better, there is no need to make the intermediate list of all lines, you can say
lines = [ line for line in open("y.sql") if not s_ora.search(line) ]

In Python 2.4 you don't have to make a list at all, you can just say
open("z.sql","w").writelines(line for line in open("y.sql") if not s_ora.search(line))

;)

Kent
except IndexError:
open("z.sql","w").writelines(lines)

but output is:

SET2_S_W CHAR(1) NOT NULL,
SET4_S_W CHAR(1) NOT NULL,
;

It should delete every, not every other!

thx,

RasDJ

Jul 18 '05 #4

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

Similar topics

1
by: D. Lee Christopher | last post by:
Can anyone point me to a good tutorial for creating a site-level search applet? I am trying to create a virtual catalog of sorts, and I would like to be able to search the catalog and have the...
5
by: Ravi | last post by:
I recently had to write some code which required me to use a binary search tree (BST) due to running time concerns. I found that there is no BST class in the Standard C++ library. I have been told...
7
by: Alec | last post by:
After trial, error, 2 days reading about PHP and some fine Single Malt, have finally created a search from a database with multiple criteria as below. NOTE comments on tidying the code up are...
60
by: Julie | last post by:
What is the *fastest* way in .NET to search large on-disk text files (100+ MB) for a given string. The files are unindexed and unsorted, and for the purposes of my immediate requirements, can't...
5
by: Martien van Wanrooij | last post by:
I have been using phpdig in some websites but now I stored a lot of larger texts into a mysql database. In the phpdig search engine, when you entered a search word, the page where the search word...
8
by: Neil Cerutti | last post by:
I can't find the mystic search glob that will find a binary search function in the Python documentation. Am I searching in vain, or do I need better search-fu? -- Neil Cerutti
3
by: suek | last post by:
I have a table with over 4000 records to search upon, and the users don't like a combo box. So what I have been trying to do for the last twelve hours is do some code to get a text box to search. ...
13
by: jfarthing | last post by:
Hi everyone! I am using the script below to search a db. If the is more than one match in the db, all goes well. But if there is only one match in the db, nothing gets displayed. Any...
8
maxx233
by: maxx233 | last post by:
I'm trying to get my program checking Active Directory to see if the user is a member of certain groups. I got it working, tested, etc. So I was very surprised when I deployed the program (just...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.