简体   繁体   English

替换字符串Python的某些部分

[英]Replacing Certain Parts of a String Python

I can not seem to solve this. 我似乎无法解决这个问题。 I have many different strings, and they are always different. 我有很多不同的字符串,而且它们总是不同的。 I need to replace the ends of them though, but they are always different lengths. 我需要替换它们的末端,但是它们的长度总是不同的。 Here is a example of a couple strings: 这是几个字符串的示例:

string1 = "thisisnumber1(111)"
string2 = "itsraining(22252)"
string3 = "fluffydog(3)"

Now when I print these out it will of course print the following: 现在,当我打印出这些内容时,它当然会打印出以下内容:

thisisnumber1(111)
itsraining(22252)
fluffydog(3)

What I would like it to print though is the follow: 我想要打印的内容如下:

thisisnumber1
itsraining
fluffydog

I would like it to remove the part in the parentheses for each string, but I do not know how sense the lengths are always changing. 我希望删除每个字符串的括号中的部分,但是我不知道长度总是在变化的意义。 Thank You 谢谢

You can use str.rsplit for this: 您可以为此使用str.rsplit

>>> string1 = "thisisnumber1(111)"
>>> string2 = "itsraining(22252)"
>>> string3 = "fluffydog(3)"
>>>
>>> string1.rsplit("(")
['thisisnumber1', '111)']
>>> string1.rsplit("(")[0]
'thisisnumber1'
>>>
>>> string2.rsplit("(")
['itsraining', '22252)']
>>> string2.rsplit("(")[0]
'itsraining'
>>>
>>> string3.rsplit("(")
['fluffydog', '3)']
>>> string3.rsplit("(")[0]
'fluffydog'
>>>

str.rsplit splits the string from right-to-left rather than left-to-right like str.split . str.rsplit将字符串从右str.rsplit拆分,而不是像str.split那样str.split向右str.split So, we split the string from right-to-left on ( and then retrieve the element at index 0 (the first element). This will be everything before the (...) at the end of each string. 因此,我们在(上从右到左分割字符串,然后检索索引0 (第一个元素)处的元素。这将是每个字符串末尾(...)之前的所有内容。

Your other option is to use regular expressions, which can give you more precise control over what you want to get. 您的另一个选择是使用正则表达式,它可以使您更精确地控制要获取的内容。

import re
regex = regex = r"(.+)\(\d+\)"

print re.match(regex, string1).groups()[0] #returns thisisnumber1
print re.match(regex, string2).groups()[0] #returns itsraining
print re.match(regex, string3).groups()[0] #returns fluffydog

Breakdown of what's happening: 发生的事件的细分:

regex = r"(.+)\\(\\d+\\)" is the regular expression, the formula for the string you're trying to find regex = r"(.+)\\(\\d+\\)"是正则表达式,即您要查找的字符串的公式

.+ means match 1 or more character of any kind except newline .+表示匹配换行符(除了换行符)之外的任何一种或多种字符

\\d+ means match 1 or more digit \\d+表示匹配1个或多个数字

\\( and \\) are the "(" and ")" characters \\(\\)是“(”和“)”字符

putting .+ in parentheses puts that string sequence in a group, meaning that group of characters is one that you want to be able to access later on. .+放在括号中会将字符串序列放在一组中,这意味着您希望以后可以使用一组字符。 We don't put the sequence \\(\\d+\\) in a group because we don't care about those characters. 我们不会将序列\\(\\d+\\)放在组中,因为我们不在乎那些字符。

regex.match(regex, string1).groups() gives every substring in string1 that was part of a group. regex.match(regex, string1).groups()给出string1中属于组的每个子字符串。 Since you only want 1 substring, you just access the 0th element. 由于只需要1个子字符串,因此只需访问第0个元素。

There's a nice tutorial on regular expressions on Tutorial's Point here if you want to learn more. 如果您想了解更多, 这里有一个很好的关于正则表达式的教程。

Since you say in a comment: 由于您在评论中说:

"all that will be in the parentheses will be numbers" “括号中所有的都是数字”

so you'll always have digits between your parens, I'd recommend taking a look at removing them with the regular expression module: 因此,您的括号之间总是有数字,我建议您看一下使用正则表达式模块将其删除的方法:

import re

string1 = "thisisnumber1(111)"
string2 = "itsraining(22252)"
string3 = "fluffydog(3)"

strings = string1, string2, string3

for s in strings:
    s_replaced = re.sub(
        r'''
        \( # must escape the parens, since these are special characters in regex
        \d+ # one or more digits, 0-9
        \)
        ''', # this regular expression will be replaced by the next argument
        '', replace the above with an empty string
        s, # the string we're modifying
        re.VERBOSE) # verbose flag allows us to comment regex clearly
    print(s_replaced)

prints: 打印:

thisisnumber1
itsraining
fluffydog

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

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