Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Multiple Replacement
#1
I would like to be able to do in Python what is so simple in Perl:

$name = $filename_in_loop_input
$name =~ s/ epub//ig;
$name =~ s/ pdf//ig;
$name =~ s/\)$//ig;
$name =~ s/\s$//ig;
$name =~ s/\s\s/ /ig;
$name =~ s/(\-\w)/ $1/ig;  # -x to - x
$name =~ s/(\w\-)/$1 /ig;  # x- to x -
$name =~ s/\./ /g;
But I seem to be running into problems.

The closest I come is
import re

fh = open('file.txt', 'rt')  
for line in fh.readlines():
    line.strip()  
    a = line 
    a = (re.sub('(?i)we','YOU',line))     # sub1
    a = (re.sub('(?i)lose','WIN',line))   # sub2
    print(a)
fh.close()
This works but only with the regex at sub2. Sub1 is ignored. I need something that will modify the variable of a file being parsed, in as simple a form as possible. The regex module seems promisingly close to PCRE for the complex stuff, but for now need something simple but effective for *simply* adding and modifying large amounts of regex substitutions. Some of my perl scripts have dozens to hundreds so that dict methods of doing this are unappealing, especially as they heavily rely on regexes.

I dont care if a solution to the problem is non-compliant with unicode. All my scripts and filenames are straight ascii anyway, plus I can use either v 3.7 or v 2.7.15
Reply
#2
I am not sure what expected final outcome is, but few observations
a = line 
a = (re.sub('(?i)we','YOU',line))     # sub1
a = (re.sub('(?i)lose','WIN',line))   # sub2
you apply each substitution on the original string line and assign result to same variable a. Not surprisingly what you get is just the last substitution.
probably you want
a = line 
a = re.sub('(?i)we','YOU', a)     # sub1
a = re.sub('(?i)lose','WIN', a)   # sub2
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
I am not familiar with Perl code so I have very little understanding what the expected result is. However, if it's about replacement then in simple cases simple str.replace would do:

>>> line = 'Why we never lose?'
>>> line.replace('we', 'YOU').replace('lose', 'WIN')
'Why YOU never WIN?'
>>> line = 'Why we never lose?'
>>> for replacement in (('we', 'YOU'), ('lose', 'WIN')):
...     line = line.replace(*replacement)
...
>>> line
'Why YOU never WIN?'
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#4
@buran :
It works! Frabjous day!
I had overlooked the bloody obvious....

@perfringo:
line.replace looks very useful for simple expressions, but re.sub seems closer to the PCRE that my perl scripts are in. As I have each replace on its own line, ideally it would enable in the future a basic 'translation' script.

While in Linux Perl is fine, in Win it occasional balks at some file/io operations that Python/Ruby handle fine. I like Ruby but it does not seem to have the raw power of Python, and eventually I need to be able to parse and categorize and manipulate around half a million items at a time.
Reply
#5
You can define the regex and the replacement in a nested list and iterate over them.


import re


replacements = [
    (r'(?i)we', 'YOU',),
    (r'(?i)lose', 'WIN'),
]


text = input('Please input some text: ')
for reg, rep in replacements:
    text = re.sub(reg, rep, text)
print(text)
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#6
Thanks for the reminder on the raw string. Needed to avoid unnecessary escapes....

The main question at this point is if there are any particular limits to input for the data variable. I will be feeding it with readline, and OS module derived path and file names. And the output of re.sub is going to be used to change data in filenames, directories, as well as file data. I'm pretty sure it will be smooth sailing, but its always good to double check with a new language.

I've already run into some minor n00b fubars, and need a bit of firmer footing before I launch into file/io where a typo can (and has) wiped hundreds of files.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Input Replacement palmtrees 7 7,190 Nov-07-2016, 06:55 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020