简体   繁体   English

Python 去掉字符串中间的数字

[英]Python remove digits in the middle of the string

I am trying to iterate through the items in python and remove the timestamp but keep the extension我正在尝试遍历 python 中的项目并删除时间戳但保留扩展名

for item in items:
    print(item.split('_')[0])

Although this works but it deletes the extension as well.虽然这有效,但它也会删除扩展名。 This how the string looks like dataset_2020-01-05.txt and this how i need it to be dataset.txt or dataset_2020-01-05.zip -> dataset.zip这就是字符串看起来像 dataset_2020-01-05.txt 的样子,这就是我需要它成为 dataset.txt 或 dataset_2020-01-05.zip -> dataset.zip 的方式

I also tried this way我也试过这种方式

for item in items:
        print(item.split('_')[0] + item.split('.')[-1])

but there are some files that doesn't have timestamp and it appends.txt to those files as well, so i ended up having something like dataset.txt.txt但是有些文件没有时间戳,它也将.txt 附加到这些文件中,所以我最终得到了类似 dataset.txt.txt 的东西

for item in items:
        front, ext = item.split('.')
        print(front.split('_')[0] + '.' + ext)

or或者

for item in items:
        ext = item.split('.')[-1]
        front = item.split('.')[0].split('_')[0]
        print(front + '.' + ext)

I would say if you have a date range, then maybe check if the date is present, and if it is present then apply the logic.我会说如果你有一个日期范围,那么也许检查日期是否存在,如果存在,然后应用逻辑。

for example: if all your files contain '2020' check例如:如果您的所有文件都包含“2020”检查
if '2020' in items

You can utilise the RE module to help with this.您可以利用 RE 模块来帮助解决此问题。 For example:例如:

import re

print(re.sub('[0-9_-]', '', 'dataset_2020-01-05.txt'))

Output: Output:

dataset.txt

To remove, match the date expression using the re module, and remove from the items array.要删除,请使用 re 模块匹配日期表达式,然后从 items 数组中删除。

import re
items = ["dataset_2020-01-05.txt", "dataset_2020-01-05.zip", "dataset.txt"]
for i, item in enumerate(items):
    match = re.search(r'_\d{4}-\d{2}-\d{2}', item)
    if(match):
        items[i] = item.replace(match.group(), '')
print(items)

Output Output

['dataset.txt', 'dataset.zip', 'dataset.txt']

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

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