简体   繁体   English

如何删除txt文件中字母后跟数字的所有实例(python)

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

I have a file in the following format:我有一个格式如下的文件:

G1 X113.901 Y66.179 E225.40813 G1 X113.901 Y66.179 E225.40813
G1 X114.212 Y66.611 E227.87255 G1 X114.212 Y66.611 E227.87255
G1 X114.396 Y67.11 E230.33708 G1 X114.396 Y67.11 E230.33708
G1 X114.452 Y67.78 E233.45031 G1 X114.452 Y67.78 E233.45031
G1 X114.057 Y71.888 E252.56566 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.它来自切片机,我想在 3D 打印机中使用它,但需要删除带有Exxx.xxxxx的部分。 Is there a way in python to read the text file and remove just this part? python 中有没有办法读取文本文件并仅删除这部分?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM