Posts: 56
Threads: 23
Joined: Jul 2021
Aug-06-2021, 08:31 PM
(This post was last modified: Aug-08-2021, 06:03 PM by AlphaInc.)
Hello everybody,
I have a text-file with multiple strings which I want to be replaced by an increasing number.
For example there is there the string "example" 7 times in it and I want to replace "example" with a number:
Before:
This example is an example on how to make an example with the word example. the example ends here with an example. for example.
After
This 1 is an 2 on how to make an 3 with the word 4. the 5 ends here with an 6. for 7.
How do you do that?
Also, while developing the script, I want it to look for every text-file inside my folder not just a specified file (see code below).
This is what I have now but it doens't work:
#!/usr/bin/env python3
fileCounter = 1
with open('*.txt', 'r') as file :
filedata = file.read()
filedata = filedata.replace('example', ''+fileCounter)
with open('*.txt', 'w') as file:
file.write(filedata)
Posts: 1,569
Threads: 3
Joined: Mar 2020
If it's not too huge, I'd just loop over it. But for very large files, that's inefficient.
paragraph = """This example is an example on how to make an
example with the word example. the example ends here with an example. for example"""
target = "example"
counter = 0
while target in paragraph:
counter += 1
paragraph = paragraph.replace(target, str(counter), 1)
print(paragraph) If it is huge, time to break out regex parser. I think there should be a simpler way to write a callable incrementor, but this will do for now.
import re
target = "example"
def str_counter(match_object):
str_counter.count += 1
return str(str_counter.count)
str_counter.count = 0
paragraph = """This example is an example on how to make an
example with the word example. the example ends here with an example. for example"""
paragraph = re.sub(re.escape(target), str_counter, paragraph)
print(paragraph)
Posts: 56
Threads: 23
Joined: Jul 2021
(Aug-06-2021, 09:05 PM)bowlofred Wrote: If it's not too huge, I'd just loop over it. But for very large files, that's inefficient.
paragraph = """This example is an example on how to make an
example with the word example. the example ends here with an example. for example"""
target = "example"
counter = 0
while target in paragraph:
counter += 1
paragraph = paragraph.replace(target, str(counter), 1)
print(paragraph) If it is huge, time to break out regex parser. I think there should be a simpler way to write a callable incrementor, but this will do for now.
import re
target = "example"
def str_counter(match_object):
str_counter.count += 1
return str(str_counter.count)
str_counter.count = 0
paragraph = """This example is an example on how to make an
example with the word example. the example ends here with an example. for example"""
paragraph = re.sub(re.escape(target), str_counter, paragraph)
print(paragraph)
Hey, thanks for replying.
Where do I have to define that my script should check in text-files? And what's the difference between target and paragraph?
Posts: 994
Threads: 15
Joined: Dec 2016
Aug-06-2021, 09:16 PM
(This post was last modified: Aug-06-2021, 09:17 PM by Axel_Erfurt.)
maybe like this?
mytext = """hello example
example test
world example
example to learn
best example
example new
better example"""
result = ""
filedata = mytext.splitlines()
i = 1
for line in filedata:
if 'example' in line:
line = line.replace("example", f"example_{str(i)}")
i += 1
result += f"{line}\n"
print(result) Output: hello example_1
example_2 test
world example_3
example_4 to learn
best example_5
example_6 new
better example_7
Posts: 1,569
Threads: 3
Joined: Mar 2020
(Aug-06-2021, 09:13 PM)AlphaInc Wrote: Where do I have to define that my script should check in text-files?
In my example, the text is already in a string called paragraph . In your example, you read in the text file to a variable called filedata . They're both just strings. Rename them how you want.
Quote:And what's the difference between target and paragraph?
One is what you're looking to replace, one is where you're looking and replacing it. Did you try running the code?
Posts: 56
Threads: 23
Joined: Jul 2021
(Aug-06-2021, 09:36 PM)bowlofred Wrote: (Aug-06-2021, 09:13 PM)AlphaInc Wrote: Where do I have to define that my script should check in text-files?
In my example, the text is already in a string called paragraph . In your example, you read in the text file to a variable called filedata . They're both just strings. Rename them how you want.
Quote:And what's the difference between target and paragraph?
One is what you're looking to replace, one is where you're looking and replacing it. Did you try running the code?
Yeah I did but it did not change my text-file. I changed it a little bit to this.
import re
target = "example"
def str_counter(match_object):
str_counter.count += 1
return str(str_counter.count)
str_counter.count = 0
with open('text.txt', 'r') as file :
filedata = file.read()
filedata = re.sub(re.escape(target), str_counter, filedata)
print(filedata) This works fine except for one last thing - how can I save the printed output to the same file and overwrite it?
Posts: 2,164
Threads: 35
Joined: Sep 2016
Aug-06-2021, 10:11 PM
(This post was last modified: Aug-06-2021, 10:20 PM by Yoriz.)
paragraph = (
"This example is an example on how to make an example with the"
" word example. the example ends here with an example. for example"
)
target = "example"
def replace_occurence_count(paragraph: str, target: str) -> str:
occurances = paragraph.count(target)
for number in range(1, occurances + 1):
paragraph = paragraph.replace(target, f"{number}", 1)
return paragraph
print(replace_occurence_count(paragraph, target)) Output: This 1 is an 2 on how to make an 3 with the word 4. the 5 ends here with an 6. for 7
@ bowlofred
For a simple counter, you can use itertools count
from itertools import count
counter = count(1)
print(next(counter))
print(next(counter)) Output: 1
2
Posts: 1,569
Threads: 3
Joined: Mar 2020
(Aug-06-2021, 10:11 PM)Yoriz Wrote: @bowlofred
For a simple counter, you can use itertools count
Yes. But for re.sub() , I needed a callable that returns the answer, not an iterator. I imagine there is something in functools that will create such a function for me, but I hadn't yet found such a solution.
Posts: 1,569
Threads: 3
Joined: Mar 2020
Aug-06-2021, 11:00 PM
(This post was last modified: Aug-06-2021, 11:00 PM by bowlofred.)
(Aug-06-2021, 10:00 PM)AlphaInc Wrote: This works fine except for one last thing - how can I save the printed output to the same file and overwrite it?
In your code you read the data from the file into a variable with <file>.read(). You can do the opposite (on a file that has been opened for writing) with <file>.write().
# read in filedata
with open('text.txt', 'r') as file :
filedata = file.read()
# do stuff with filedata here
# write filedata out here (overwriting the file)
with open('text.txt', 'w') as file:
file.write(filedata) This is a bit dangerous because for a period of time, there is no data on disk. For a production script, I'd change this to write the file to another name, then after the write has succeeded, rename the file.
Posts: 2,164
Threads: 35
Joined: Sep 2016
(Aug-06-2021, 10:55 PM)bowlofred Wrote: Yes. But for re.sub() , I needed a callable that returns the answer, not an iterator. I imagine there is something in functools that will create such a function for me, but I hadn't yet found such a solution. Call it with next as I did in the example above.
|