如何使用 python 拆分文件名

how to split the filename using python

我正在使用 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 文件创建名称时删除 _rev1_2021_03_09.zip

这是我的 python 代码。它适用于 Retirement_participant-plan_info_v1_getPlankeys_rev1_2021_03_09.zip,但如果我的 zip 文件名称太大,例如 Retirement_participant-plan_info_resetcache_param_v1_balances_rev1_2021_03_09.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) 

您可以 str.split 通过 rev1_

>>> 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']

同样可以通过 str.rsplitmaxsplit 限制为 4 来实现:

>>> 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']

如果 rev 和 date 始终相同 (2021_03_09),只需将它们替换为空字符串:

filenames = [f.replace("_rev1_2021_03_09.zip", "") for f in os.listdir(path)]