Hi, there

I have some lines in a text file as
159 J=1661,3169,1679,3181 SEC=SLAB2
66 J=5597,5596,7523,7522 SEC=WALL1

How can I delete "SEC=SLAB2" or "SEC=WALL1" and modify the roginal lines into new ones, if I have RE functions
159 J=1661,3169,1679,3181
66 J=5597,5596,7523,7522

I am using and stuyding Python. Please forget me to ask some simple questions here. Thank you so much.

Best Wishes

Ning

Here would be one way to do this:

text = """\
159 J=1661,3169,1679,3181 SEC=SLAB2
66 J=5597,5596,7523,7522 SEC=WALL1"""

print(text)

print('-'*35)

# create a list of words excluding item containing 'SEC=" 
temp_list = [item for line in text.split('\n')
    for item in line.split()
    if not 'SEC=' in item]

#print temp_list  # test

# rebuild the text from the list
mod_text = ""
for ix, word in enumerate(temp_list):
    if ix % 2:
        # add newline
        mod_text += word + '\n'
    else:
        # add space
        mod_text += word + " "

print(mod_text)
    
"""
my result -->
159 J=1661,3169,1679,3181 SEC=SLAB2
66 J=5597,5596,7523,7522 SEC=WALL1
-----------------------------------
159 J=1661,3169,1679,3181
66 J=5597,5596,7523,7522
"""

Using module re makes it look a little simpler:

import re

text = """\
159 J=1661,3169,1679,3181 SEC=SLAB2
66 J=5597,5596,7523,7522 SEC=WALL1"""

print(text)

print('-'*35)

# if SEC= is followed by cap letters and and numbers
p = re.compile( '(SEC=[A-Z, 1-9]*)')
mod_text = p.sub( '', text)

print(mod_text)

"""
my result -->
159 J=1661,3169,1679,3181 SEC=SLAB2
66 J=5597,5596,7523,7522 SEC=WALL1
-----------------------------------
159 J=1661,3169,1679,3181
66 J=5597,5596,7523,7522
"""
Member Avatar for leegeorg07

if you knew what line and what you want to change and to what wouldn't it be easier to use:

import string
text = """159 J=1661,3169,1679,3181 SEC=SLAB2
66 J=5597,5596,7523,7522 SEC=WALL1"""

string.replace(text, "SEC=SLAB2", "SEC=SLAB3")

which will replace SEC=SLAB2 with SEC=SLAB3, you can change it all to replace different things

Here would be one way to do this:

text = """\
159 J=1661,3169,1679,3181 SEC=SLAB2
66 J=5597,5596,7523,7522 SEC=WALL1"""

print(text)

print('-'*35)

# create a list of words excluding item containing 'SEC=" 
temp_list = [item for line in text.split('\n')
    for item in line.split()
    if not 'SEC=' in item]

#print temp_list  # test

# rebuild the text from the list
mod_text = ""
for ix, word in enumerate(temp_list):
    if ix % 2:
        # add newline
        mod_text += word + '\n'
    else:
        # add space
        mod_text += word + " "

print(mod_text)
    
"""
my result -->
159 J=1661,3169,1679,3181 SEC=SLAB2
66 J=5597,5596,7523,7522 SEC=WALL1
-----------------------------------
159 J=1661,3169,1679,3181
66 J=5597,5596,7523,7522
"""

Using module re makes it look a little simpler:

import re

text = """\
159 J=1661,3169,1679,3181 SEC=SLAB2
66 J=5597,5596,7523,7522 SEC=WALL1"""

print(text)

print('-'*35)

# if SEC= is followed by cap letters and and numbers
p = re.compile( '(SEC=[A-Z, 1-9]*)')
mod_text = p.sub( '', text)

print(mod_text)

"""
my result -->
159 J=1661,3169,1679,3181 SEC=SLAB2
66 J=5597,5596,7523,7522 SEC=WALL1
-----------------------------------
159 J=1661,3169,1679,3181
66 J=5597,5596,7523,7522
"""

Hi, leegeorg07

Thank you a lot. I am using your second code, which is exactly what I want to do.

all the best

ning

if you knew what line and what you want to change and to what wouldn't it be easier to use:

import string
text = """159 J=1661,3169,1679,3181 SEC=SLAB2
66 J=5597,5596,7523,7522 SEC=WALL1"""

string.replace(text, "SEC=SLAB2", "SEC=SLAB3")

which will replace SEC=SLAB2 with SEC=SLAB3, you can change it all to replace different things

Hi, sneekula

Thank you so much. Unfortunately I have no idea about what and where I will change.

all the best

ning

Member Avatar for leegeorg07

thats no problem but one note

you gave me the credit for the better code, i cant do re so i just think that sneekula should get the credit he deserves

Hi, leegeorg07

I just realised this credit system with your reminder. Sorry about that.

Have a nice weeked.

Ning

Here would be one way to do this:

text = """\
159 J=1661,3169,1679,3181 SEC=SLAB2
66 J=5597,5596,7523,7522 SEC=WALL1"""

print(text)

print('-'*35)

# create a list of words excluding item containing 'SEC=" 
temp_list = [item for line in text.split('\n')
    for item in line.split()
    if not 'SEC=' in item]

#print temp_list  # test

# rebuild the text from the list
mod_text = ""
for ix, word in enumerate(temp_list):
    if ix % 2:
        # add newline
        mod_text += word + '\n'
    else:
        # add space
        mod_text += word + " "

print(mod_text)
    
"""
my result -->
159 J=1661,3169,1679,3181 SEC=SLAB2
66 J=5597,5596,7523,7522 SEC=WALL1
-----------------------------------
159 J=1661,3169,1679,3181
66 J=5597,5596,7523,7522
"""

Using module re makes it look a little simpler:

import re

text = """\
159 J=1661,3169,1679,3181 SEC=SLAB2
66 J=5597,5596,7523,7522 SEC=WALL1"""

print(text)

print('-'*35)

# if SEC= is followed by cap letters and and numbers
p = re.compile( '(SEC=[A-Z, 1-9]*)')
mod_text = p.sub( '', text)

print(mod_text)

"""
my result -->
159 J=1661,3169,1679,3181 SEC=SLAB2
66 J=5597,5596,7523,7522 SEC=WALL1
-----------------------------------
159 J=1661,3169,1679,3181
66 J=5597,5596,7523,7522
"""

Hi, sneekula

I did not realised this working credit system. Soory about that. Hopefully this time will compensate somthing.

Have a nice weekend

ning

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.