Reputation: 13
I'm new to Python and Regex patterns and it's been very frustrating. I'd be very much appreciated if someone can assist me in finding the correct patten to remove three things (1) ',' (2) '] (3) ['
with open(f"/home/pi/students/{biometric_id}.txt") as f:
content = f.readlines()
content = str([item.replace('\n', '<br>') for item in content])
#content = re.sub(r"\([^()]*\)", "", content)
return render_template("Page-4.html", content=content)
[' ', ' ', ' ', 'Item: Cheese Wrap ', 'Cost: $3.00 ', 'Remaining Balance: $12.2 ', '-------------------------- ']
Upvotes: 1
Views: 686
Reputation: 71
When you want to validate your regex I recommend you to use: https://regexr.com/
About your question, try with
content = "Hola, (mundo)"
re.sub(r'[,()]', '', content)
>> 'Hola mundo'
What do you think?
Upvotes: 0
Reputation: 481
I think what you have here is a file with list items.
So what you do is to read it in and "evaluate" them as list items:
with open(f"/home/pi/students/{biometric_id}.txt", 'r') as f:
for line in f:
data = eval(line)
for item in data:
# do something useful with the data
print(item)
If the file contained:
['STUDENT DETAILS - ', 'Biometric ID: 653694 ', 'RFID: 18985211235 ', 'Full Name: James John ', 'Balance: $15.2 ', '-------------------------- ', ' ', ' ', 'Item: Cheese Wrap ', 'Cost: $3.00 ', 'Remaining Balance: $12.2 ', '-------------------------- ']
The above would print out:
STUDENT DETAILS -
Biometric ID: 653694
RFID: 18985211235
Full Name: James John
Balance: $15.2
--------------------------
Item: Cheese Wrap
Cost: $3.00
Remaining Balance: $12.2
--------------------------
Upvotes: 0
Reputation: 106
Try:
content = "['STUDENT DETAILS - ', 'Biometric ID: 653694 ', 'RFID: 18985211235 ', 'Full Name: James John ', 'Balance: $15.2 ', '-------------------------- ', ' ', ' ', 'Item: Cheese Wrap ', 'Cost: $3.00 ', 'Remaining Balance: $12.2 ', '-------------------------- '] "
content = re.sub(r"(\',\s?\')|(\'\])|(\[\')", r"", content)
print(content)
Output:
STUDENT DETAILS - Biometric ID: 653694 RFID: 18985211235 Full Name: James John Balance: $15.2 -------------------------- Item: Cheese Wrap Cost: $3.00 Remaining Balance: $12.2 --------------------------
Upvotes: 1