How to delete all instances of a letter followed by a number in a txt file (python)

38 Views Asked by At

I have a file in the following format:

G1 X113.901 Y66.179 E225.40813
G1 X114.212 Y66.611 E227.87255
G1 X114.396 Y67.11 E230.33708
G1 X114.452 Y67.78 E233.45031
G1 X114.057 Y71.888 E252.56566

It's from a slicer and I want to use it in a 3D-Printer, but the part with Exxx.xxxxx needs to be removed. Is there a way in python to read the text file and remove just this part?

1

There are 1 best solutions below

0
matszwecja On

You can use regex:

import re

t = '''
G1 X113.901 Y66.179 E225.40813

G1 X114.212 Y66.611 E227.87255

G1 X114.396 Y67.11 E230.33708

G1 X114.452 Y67.78 E233.45031

G1 X114.057 Y71.888 E252.56566
'''

pattern = re.compile(r'E[0-9]+\.[0-9]+')

print(re.sub(pattern, "", t))