[英]Remove a part of string from one character to another
For the following strings in a string: 对于字符串中的以下字符串:
User Input(id=2345) : Hello
User Input(id=9423924) : Hi!
User Input(id=233123) : How's it going
I want to remove the part in the brackets. 我想删除括号中的部分。 to look something like: 看起来像:
User Input: Hello
User Input: Hi!
User Input: How's it going
I have tried the follow code: 我试过以下代码:
import re
file = file1.read()
for line in file
print(re.sub(r'\((.*?)\line)\+', '', line))
it gives me an error- any suggestions would be really helpful! 它给了我一个错误 - 任何建议都会非常有用! Thank you 谢谢
You can use regular expressions: 您可以使用正则表达式:
>>> s = 'User Input(id=2345) : Hello'
>>> import re
>>> re.sub(r'\(.+?\)', '', s)
'User Input : Hello'
This will not work super robustly for nested brackets, but for the input at hand, it should do just fine. 对于嵌套括号,这不会超级健壮,但对于手头的输入,它应该做得很好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.