如何从Python中提取文件名中的数字?

时间:2012-12-23 03:42:14

标签: python

我只需从文件名中提取数字,例如:

GapPoints1.shp

GapPoints23.shp

GapPoints109.shp

如何使用Python从这些文件中提取数字?我需要将其合并到for循环中。

4 个答案:

答案 0 :(得分:19)

你可以使用正则表达式:

regex = re.compile(r'\d+')

然后获取匹配的字符串:

regex.findall(filename)

这将返回包含数字的字符串列表。如果您确实需要整数,可以使用int

[int(x) for x in regex.findall(filename)]

如果每个文件名中只有1个数字,则可以使用regex.search(filename).group(0)(如果您确定它会产生匹配)。如果未找到匹配项,则上述行将生成一个AttributeError,指出NoneType没有属性group

答案 1 :(得分:5)

所以,你没有留下任何关于这些文件的位置以及如何获取这些文件的描述,但我假设您使用os模块获取文件名。

至于从名字中获取数字,你最好使用re的正则表达式,如下所示:

import re
def get_numbers_from_filename(filename):
    return re.search(r'\d+', filename).group(0)

然后,要在for循环中包含它,您将在每个文件名上运行该函数:

for filename in os.listdir(myfiledirectory):
   print get_numbers_from_filename(filename)

或类似的东西。

答案 2 :(得分:3)

如果只有一个号码:

filter(lambda x: x.isdigit(), filename)

答案 3 :(得分:0)

Hear是我的代码,在从Google Scholar下载文件后,我将论文的发表年份带到文件名的第一位。 主要文件通常是这样构造的:Author + publishedYear.pdf因此,通过实现此代码,文件名将变为:PublishedYear + Author.pdf。

# Renaming Pdf according to number extraction
# You want to rename a pdf file, so the digits of document published year comes first.
# Use regular expersion
# As long as you implement this file, the other pattern will be accomplished to your filename.

# import libraries
import re
import os

# Change working directory to this folder
address = os.getcwd ()
os.chdir(address)

# defining a class with two function
class file_name:
    # Define a function to extract any digits
    def __init__ (self, filename):
        self.filename = filename

    # Because we have tow pattern, we must define tow function.
    # First function for pattern as : schrodinger1990.pdf
    def number_extrction_pattern_non_digits_first (filename):

        pattern = (r'(\D+)(\d+)(\.pdf)')
        digits_pattern_non_digits_first = re.search(pattern, filename, re.IGNORECASE).group (2) 
        non_digits_pattern_non_digits_first = re.search(pattern, filename, re.IGNORECASE).group (1)
        return digits_pattern_non_digits_first, non_digits_pattern_non_digits_first

    # Second function for pattern as : 1993schrodinger.pdf
    def number_extrction_pattern_digits_first (filename):

        pattern = (r'(\d+)(\D+)(\.pdf)')
        digits_pattern_digits_first = re.search(pattern, filename, re.IGNORECASE).group (1) 
        non_digits_pattern_digits_first = re.search(pattern, filename, re.IGNORECASE).group (2)

        return digits_pattern_digits_first, non_digits_pattern_digits_first



if __name__ == '__main__':

    # Define a pattern to check filename pattern
    pattern_check1 = (r'(\D+)(\d+)(\.pdf)')

    # Declare each file address.
    for filename in os.listdir(address):

        if filename.endswith('.pdf'):
            if re.search(pattern_check1, filename, re.IGNORECASE):

                digits = file_name.number_extrction_pattern_non_digits_first (filename)[0]
                non_digits = file_name.number_extrction_pattern_non_digits_first (filename)[1]
                os.rename(filename, digits + non_digits + '.pdf')

            # Else other pattern exists.    
            else :

                digits = file_name.number_extrction_pattern_digits_first (filename)[0]
                non_digits = file_name.number_extrction_pattern_digits_first (filename)[1]
                os.rename(filename, digits + non_digits + '.pdf')