Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.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_Python 3.x_Xml_Python 2.7 - Fatal编程技术网

如何使用python拆分文件名

如何使用python拆分文件名,python,python-3.x,xml,python-2.7,Python,Python 3.x,Xml,Python 2.7,我正在使用python使用元素和子元素进程创建xml文件。 我的文件夹中有一个zip文件列表,如下所示: Retirement_participant-plan_info_v1_getPlankeys_rev1_2021_03_09.zip Retirement_participant-plan_info_resetcache_secretmanager_rev1_2021_03_09.zip Retirement_participant-plan_info_v1_mypru_plankeys_

我正在使用python使用元素和子元素进程创建xml文件。 我的文件夹中有一个zip文件列表,如下所示:

Retirement_participant-plan_info_v1_getPlankeys_rev1_2021_03_09.zip
Retirement_participant-plan_info_resetcache_secretmanager_rev1_2021_03_09.zip
Retirement_participant-plan_info_v1_mypru_plankeys_rev1_2021_03_09.zip
Retirement_participant-plan_info_resetcache_param_value_rev1_2021_03_09.zip
Retirement_participant-plan_info_resetcache_param_v1_balances_rev1_2021_03_09.zip

我想拆分这些zip文件并获得如下名称:

Retirement_participant-plan_info_v1_getPlankeys
Retirement_participant-plan_info_resetcache_secretmanager
Retirement_participant-plan_info_v1_mypru_plankeys
Retirement_participant-plan_info_resetcache_param_value
Retirement_participant-plan_info_resetcache_param_v1_balances
PS:我想在从zip文件创建名称时删除
\u rev1\u 2021\u 03\u 09.zip

这是我的python代码。它适用于
退休计划参与者信息v1\u getPlankeys\u rev1\u 2021\u 03\u 09.zip
但如果我有太大的名字来存放例如
退休计划参与者信息重置缓存参数v1\u balances\u rev1\u 2021\u 03\u 09.zip的zip文件,它就不起作用了

    Proxies = SubElement(proxy, 'Proxies')
    path = "./"
    for f in os.listdir(path):
        if '.zip' in f:
            Proxy = SubElement(Proxies, 'Proxy')
            name = SubElement(Proxy, 'name')
            fileName = SubElement(Proxy, 'fileName')
            a = f.split('_')
            name.text = '_'.join(a[:3])
            fileName.text = str(f) 

您可以通过
rev1\uu

>>> filenames

['Retirement_participant-plan_info_v1_getPlankeys_rev1_2021_03_09.zip',
 'Retirement_participant-plan_info_resetcache_secretmanager_rev1_2021_03_09.zip',
 'Retirement_participant-plan_info_v1_mypru_plankeys_rev1_2021_03_09.zip',
 'Retirement_participant-plan_info_resetcache_param_value_rev1_2021_03_09.zip',
 'Retirement_participant-plan_info_resetcache_param_v1_balances_rev1_2021_03_09.zip']

>>> names = [fname.split('_rev1_')[0] for fname in filenames]

>>> names

['Retirement_participant-plan_info_v1_getPlankeys',
 'Retirement_participant-plan_info_resetcache_secretmanager',
 'Retirement_participant-plan_info_v1_mypru_plankeys',
 'Retirement_participant-plan_info_resetcache_param_value',
 'Retirement_participant-plan_info_resetcache_param_v1_balances']
通过将
maxplit
限制为
4
,同样可以使用
str.rsplit
实现:

>>> names = [fname.rsplit('_', 4)[0] for fname in filenames]
>>> names
['Retirement_participant-plan_info_v1_getPlankeys',
 'Retirement_participant-plan_info_resetcache_secretmanager',
 'Retirement_participant-plan_info_v1_mypru_plankeys',
 'Retirement_participant-plan_info_resetcache_param_value',
 'Retirement_participant-plan_info_resetcache_param_v1_balances']

如果版本和日期始终相同(
2021\u 03\u 09
),只需将其替换为空字符串:

filenames=[f.replace(“\u rev1\u 2021\u 03\u 09.zip”,”)用于os.listdir(path)中的f

您希望从末端剥离的位是否始终相同,即
\u rev1\u 2021\u 03\u 09.zip
?或者它是否有所不同,但始终遵循这种模式,例如,在另一天它可能是
\u rev8\u 2021\u 03\u 11.zip
?我认为问题在于最后第二行:
name.text=''.'''.join(a[:3])
。目前,它只是从
\uu
拆分中提取前3个片段。由于文件名在开始时有不同的长度,这有时会切断部分文件名。由于结尾是一致的,您可以将
3
更改为
-4
,这将占用最后四段的所有内容。谢谢。这确实奏效了。我刚刚用您说的逻辑更新了我的代码:
for f in os.listdir(path):if.zip in f:Proxy=SubElement(Proxy,'Proxy')name=SubElement(Proxy,'name')fileName=SubElement(Proxy,'fileName')a=f.split('u rev1')[0]#a=f.splitname.text=a#name.text='''.'''.'连接(a[:5])fileName.text=str(f)
我们可以在这个逻辑上使用argparse定义一个变量吗
names=[fname.split('u rev1')[0]用于文件名中的fname]``使用
names=[fname.split('repa.version')[0]用于文件名中的fname]``这对我不起作用。你有更好的建议吗!