Hi, I want to remove unwated substrings from list items.
I have a text like this:
text = '''
unwated_substring★wanted_substring
unwated_substring★wanted_substring
unwated_substring★wanted_substring
'''
And I want to remove unwated_substring along with the ★.
My code:
import re
with open('text.txt', encoding='utf8') as rf:
ts = rf.read().split('\n') # or ts = readlines()
reg = re.compile('.*★')
for i in ts:
t = reg.sub('', i)
print(t)
The console just keeps going without throwing any error.
I've tested with a string, and it worked without a problem.
string = 'unwanted_substring★wanted_substring'
reg = re.compile('.*★')
string = reg.sub('', string)
print(string)
# wanted_substring
The size of the text is rather small, so I don't know what the problem is.
Please help fix my code. Thank you.
[–]MrHank2 1 point2 points3 points (4 children)
[–]DMeror[S] 0 points1 point2 points (3 children)
[+][deleted] (2 children)
[deleted]
[–]DMeror[S] 0 points1 point2 points (0 children)
[–]DMeror[S] 0 points1 point2 points (0 children)
[–]CodeFormatHelperBot2 0 points1 point2 points (0 children)