473,385 Members | 1,645 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,385 software developers and data experts.

delete lines

I am new in python and I have the following problem:

Suppose I have a list with words of which I want to remove each time
the words in the lines below item1 and above item2:

item1
a
b
item2
c
d
item3
e
f
item4
g
h
item1
i
j
item2
k
l
item3
m
n
item4
o
p

I did not find out how to do this:

Part of my script was

f = re.compile("item\[1\]\:")
g = re.compile("item\[2\]\:")
for i, line in enumerate(list1):
f_match = f.search(line)
g_match = g.search(line)
if f_match:
if g_match:
if list1[i] f_match:
if list1[i] < g_match:
del list1[i]
But this does not work

If someone can help me, thanks!
Jul 9 '08 #1
1 1050
antar2 wrote:
I am new in python and I have the following problem:

Suppose I have a list with words of which I want to remove each time
the words in the lines below item1 and above item2:
f = re.compile("item\[1\]\:")
g = re.compile("item\[2\]\:")
for i, line in enumerate(list1):
Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*f_match = f.search(line)
Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*g_match = g.search(line)
Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*if f_match:
Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*if g_match:
Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â* Â*Â*Â*Â*Â*Â*Â*if list1[i] f_match:
Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â* Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*if list1[i] < g_match:
Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â* Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*del list1[i]
But this does not work

If someone can help me, thanks!
I see two problems with your code:

- You are altering the list while you iterate over it. Don't do that, it'll
cause Python to skip items, and the result is usually a mess. Make a new
list instead.

- You don't keep track of whether you are between "item1" and "item2". A
simple flag will help here.

A working example:

inlist = """item1
a
b
item2
c
d
item3
e
f
item4
g
h
item1
i
j
item2
k
l
item3
m
n
item4
o
p
""".splitlines()

print inlist

outlist = []
between = False

for item in inlist:
if between:
if item == "item2":
between = False
outlist.append(item)
else:
outlist.append(item)
if item == "item1":
between = True

print outlist

Peter
Jul 9 '08 #2

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

Similar topics

3
by: Rolf | last post by:
I have a form with about 15 to 20 lines. Their names are LineXXX. What code can I put in the click event of a button to delete all the lines? Thanks! Rolf
1
by: nasirmajor | last post by:
dear all, Please any urgent help regarding following code. i have the following code ================================================================= public void Delete(Object sender,...
3
by: Kevin M | last post by:
I have one table and have created a form from that table. Also, I have created a delete query. I want to but a button on the form which will delete all records from the table; however, I cannot get...
2
by: Francesco Pietra | last post by:
Please, how to adapt the following script (to delete blank lines) to delete lines containing a specific word, or words? f=open("output.pdb", "r") for line in f: line=line.rstrip() if line:...
0
by: Francesco Pietra | last post by:
I forgot to add that the lines to strip are in present case of the type of the following block HETATM 7007 O WAT 446 27.622 34.356 55.205 1.00 0.00 O HETATM 7008 H1 WAT...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.