简体   繁体   English

从一个字符中删除一部分字符串到另一个字符

[英]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.

相关问题 将项目从一个列表移动到另一个列表并删除字符串的某些部分 - Move items from one list to another and remove certain part of the string 删除字符串中字符和数字连接在一起的部分 - Remove the part with a character and numbers connected together in a string 如何用一个列表中的部分字符串替换另一个列表中的另一个部分? - How to substitute part of a string from one list with another part from another list? 删除从“[”到末尾的部分字符串 - Remove part of string from "[" to the end 如果存在于另一列的字符串中,则从一列中删除字符串 pandas - Remove string from one column if present in string of another column pandas 如何从列表中的一个元素中检测字符串的某些部分,从列表中的另一个元素中检测字符串的另一部分? - How to detect some part of string from one element in list and other part from another element in list? 在字符串列表中,对于每个字符串,在特定字符之后删除字符串的一部分 - In a list of strings, for each string remove part of a string after a specific character 从字符串中删除特定字符 - Remove specific character from string 从字符串中删除不需要的字符 - Remove unwanted character from string 从字符串中删除反斜杠字符 - Remove backslash character from string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM