Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中,如果存在字符串组合,则在列表中查找索引_Python_String - Fatal编程技术网

在python中,如果存在字符串组合,则在列表中查找索引

在python中,如果存在字符串组合,则在列表中查找索引,python,string,Python,String,我正在编写我的第一个脚本并尝试学习python。 但我被卡住了,无法摆脱这一关 我正在写一个脚本来更改文件名 假设我有一个string=“this.is.tEst3.E00.erfeh.ervwer.vwtrt.rvwrv” 我希望结果是string=“这是Test3 E00” 这就是我到目前为止所做的: l = list(string) //Transform the string into list for i in l: if "E" in l:

我正在编写我的第一个脚本并尝试学习python。 但我被卡住了,无法摆脱这一关

我正在写一个脚本来更改文件名

假设我有一个
string=“this.is.tEst3.E00.erfeh.ervwer.vwtrt.rvwrv”

我希望结果是
string=“这是Test3 E00”

这就是我到目前为止所做的:

l = list(string) 

//Transform the string into list

for i in l:
    if "E" in l:
        p = l.index("E")
        if isinstance((p+1), int () is True:
            if isinstance((p+2), int () is True:
                delp = p+3
                a = p-3
                del l[delp:]

new = "".join(l)
new = new.replace("."," ")
print (new)

                
进入索引“E”,检查“E”之后是否有2个整数。 然后删除第二个整数之后的所有内容

但是,如果在其他任何地方有一个“E”,这将不起作用

目前我得到的结果是:

这是测试

因为它正在为列表中的第一个“E”查找索引,并删除索引+3之后的所有内容

我想我的问题是,如果字符串的组合存在,我如何获得列表中的索引

但我似乎不知道该怎么做

谢谢大家的回答。 我本来是往另一个方向走的,但它也不起作用。 如果有人能明白为什么会很棒。在实践中学习比应付别人写的东西要好得多:)

这就是我想到的:

对于l中的i:

if i=="E" and isinstance((i+1), int ) is True:
    p = l.index(i)
    print (p)
任何人都可以告诉我为什么这不起作用。我犯了一个错误


非常感谢

一个简单的方法是使用正则表达式查找,直到
E
后跟2位标准,并使用
s
作为字符串:

import re
up_until = re.match('(.*?E\d{2})', s).group(1)
# this.is.tEst3.E00
然后,我们用空格替换
,然后将其命名为:

output = up_until.replace('.', ' ').title()
# This Is Test3 E00

一种简单的方法是使用正则表达式查找,直到
E
后跟2位标准,并将
s
作为字符串:

import re
up_until = re.match('(.*?E\d{2})', s).group(1)
# this.is.tEst3.E00
然后,我们用空格替换
,然后将其命名为:

output = up_until.replace('.', ' ').title()
# This Is Test3 E00

一种简单的方法是使用正则表达式查找,直到
E
后跟2位标准,并将
s
作为字符串:

import re
up_until = re.match('(.*?E\d{2})', s).group(1)
# this.is.tEst3.E00
然后,我们用空格替换
,然后将其命名为:

output = up_until.replace('.', ' ').title()
# This Is Test3 E00

一种简单的方法是使用正则表达式查找,直到
E
后跟2位标准,并将
s
作为字符串:

import re
up_until = re.match('(.*?E\d{2})', s).group(1)
# this.is.tEst3.E00
然后,我们用空格替换
,然后将其命名为:

output = up_until.replace('.', ' ').title()
# This Is Test3 E00

你听说过正则表达式吗

查看python的
re
模块

基本上,您可以定义一个“regex”,它将匹配“E,然后是两个整数”,并给出它的索引

之后,我将使用python的“切片表示法”来选择要保留的字符串片段


然后,检查for
str.replace
以将句点替换为空格,并检查
str.title
以将其置于标题格中

您听说过正则表达式吗

查看python的
re
模块

基本上,您可以定义一个“regex”,它将匹配“E,然后是两个整数”,并给出它的索引

之后,我将使用python的“切片表示法”来选择要保留的字符串片段


然后,检查for
str.replace
以将句点替换为空格,并检查
str.title
以将其置于标题格中

您听说过正则表达式吗

查看python的
re
模块

基本上,您可以定义一个“regex”,它将匹配“E,然后是两个整数”,并给出它的索引

之后,我将使用python的“切片表示法”来选择要保留的字符串片段


然后,检查for
str.replace
以将句点替换为空格,并检查
str.title
以将其置于标题格中

您听说过正则表达式吗

查看python的
re
模块

基本上,您可以定义一个“regex”,它将匹配“E,然后是两个整数”,并给出它的索引

之后,我将使用python的“切片表示法”来选择要保留的字符串片段


然后,检查for <代码> STR.EXT/<代码>,以交换空格的周期,以及<代码> STRITE标题将它们放在标题案例

< P>中考虑使用的技术是正则表达式。它们允许您搜索字符串中的文本模式,而不是特定的字符或子字符串。正则表达式的学习过程有点艰难,但学习起来非常宝贵,您可以在多种语言中使用它们,而不仅仅是在Python中。下面是有关如何实现正则表达式的Python资源:

在您的案例中,您希望匹配的模式是“E”后跟两个数字。在正则表达式中(通常缩写为“regex”或“regexp”),该模式如下所示:

E\d\d # ('\d' is the specifier for any digit 0-9)
import re
filename = 'this.is.tEst3.E00.erfeh.ervwer.vwtrt.rvwrv'
match_object = re.search(r'E\d\d', filename)
if match_object:
    # The '0' means we want the first match found
    index_of_Exx = match_object.end(0)
    truncated_filename = filename[:index_of_Exx]
    # Now take care of any more processing
match_object = re.search(r'E\d\d', filename, re.I)
在Python中,创建要匹配的正则表达式模式的字符串,并将该字符串和文件名字符串传递到re模块的search()方法中。正则表达式模式倾向于使用许多特殊字符,因此在Python中,在正则表达式模式字符串前面加上“r”是很常见的,这会告诉Python解释器不要将特殊字符解释为转义字符。所有这些加在一起看起来是这样的:

E\d\d # ('\d' is the specifier for any digit 0-9)
import re
filename = 'this.is.tEst3.E00.erfeh.ervwer.vwtrt.rvwrv'
match_object = re.search(r'E\d\d', filename)
if match_object:
    # The '0' means we want the first match found
    index_of_Exx = match_object.end(0)
    truncated_filename = filename[:index_of_Exx]
    # Now take care of any more processing
match_object = re.search(r'E\d\d', filename, re.I)
正则表达式可以变得非常详细(和复杂)。事实上,您可以使用一个正确组合的正则表达式来完成完全更改文件名的整个任务。但是由于我不知道你的程序中可能会出现哪些奇怪的文件名,所以我不能再进一步了。我将再添加一条信息:如果“E”可能是小写,那么您希望在模式搜索中添加一个标志作为第三个参数,它指示不区分大小写的匹配。该标志为're.I',您的search()方法如下所示:

E\d\d # ('\d' is the specifier for any digit 0-9)
import re
filename = 'this.is.tEst3.E00.erfeh.ervwer.vwtrt.rvwrv'
match_object = re.search(r'E\d\d', filename)
if match_object:
    # The '0' means we want the first match found
    index_of_Exx = match_object.end(0)
    truncated_filename = filename[:index_of_Exx]
    # Now take care of any more processing
match_object = re.search(r'E\d\d', filename, re.I)
阅读Python're'模块的文档了解更多信息,您可以在网上找到许多很棒的教程,例如:


在你意识到这一点之前,你将成为一名。:-)