简体   繁体   English

从 python 中的字符串中删除部分

[英]Remove parts from string in python

So I have a few thousand strings in a format like this:所以我有几千个字符串,格式如下:

"something - something else (another thing) [even more things]"

And I need to remove the parenthesis and square brackets but my problem is that any other part of the string could contain square brackets/parenthesis too (there could also be more square brackets/parenthesis in the square brackets, but the parenthesis could only contain square brackets and not more parenthesis) and the number of spaces is also different for each string.而且我需要删除括号和方括号,但我的问题是字符串的任何其他部分也可能包含方括号/括号(方括号中也可能有更多的方括号/括号,但括号只能包含方括号括号而不是更多的括号)并且每个字符串的空格数也不同。 The only thing that is constant is that the square brackets/parenthesis I want to remove are always at the end of the string.唯一不变的是我要删除的方括号/括号始终位于字符串的末尾。 How would I remove these without changing anything else in the string to get the output string:如何在不更改字符串中的任何其他内容的情况下删除这些以获取 output 字符串:

"something - something else"

Edit: Just to clarify the length of the string and the number of words can always be different, it's just always the same "shape", basically it's:编辑:只是为了澄清字符串的长度和单词的数量总是可以不同的,它总是相同的“形状”,基本上是:

"some unknown string" + "-" + "some unknown string" + "(some unknown string)" + "[some unknown string]"

Do you specifically need to be able to find and cut it at those brackets?您是否特别需要能够在这些括号中找到并剪切它? Or is it a case of being able to simply slice away the end of the string?还是可以简单地切掉字符串的末尾? If so, you could do it using the slice method in python like so:如果是这样,您可以使用 python 中的 slice 方法,如下所示:

` `

   str = "something - something else (another thing) [even more things]"
   str_to_cut = "something - something else"
     
    print ("Original string: " + str) 
    print(len(str_to_cut)) 
  
    # slicing string characters after position len 
    res_str = str[:21] 
       
     
    print ("String after removal of character: " + res_str) 

` `

If your input does not contain parenthesis inside your square brackets, you could just userfind() to find the position of the last "(" occurring in the string. Then, slicing the initial string until that position (minus one to account for the space) will give you the desired output:如果您的输入不包含方括号内的括号,您可以使用rfind()来查找字符串中最后一个"("的 position。然后,将初始字符串切片直到 position(减去一来说明空间)将为您提供所需的 output:

s = "something - something else (another thing) [even more things]"
print(repr(s[:s.rfind("(") - 1]))
# 'something - something else'

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

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