1

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 ', '-------------------------- ']

4
  • show more example of sample data
    – Ade_1
    Feb 23, 2021 at 6:27
  • ['STUDENT DETAILS - ', 'Biometric ID: 653694 ', 'RFID: 18985211235 ', 'Full Name: James John ', 'Balance: $15.2 ', '-------------------------- ', ' ', ' ', 'Item: Cheese Wrap ', 'Cost: $3.00 ', 'Remaining Balance: $12.2 ', '-------------------------- ']
    – Mitch
    Feb 23, 2021 at 6:30
  • Regex CODE Feb 23, 2021 at 6:41
  • Thank you, I didn't realise there was an Regex Expression Builder :-)
    – Mitch
    Feb 23, 2021 at 6:56

3 Answers 3

1

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 --------------------------  
0
0

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
--------------------------
1
  • you are correct that there are better ways of parsing this list data. however, eval really is dangerous. consider ast.literal_eval or other parsing routines Feb 23, 2021 at 7:03
0

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?

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.