12

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

GapPoints1.shp

GapPoints23.shp

GapPoints109.shp

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

4

4 回答 4

23

您可以使用正则表达式:

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

然后获取匹配的字符串:

regex.findall(filename)

这将返回一个包含数字的字符串列表。如果你真的想要整数,你可以使用int

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

如果每个文件名中只有 1 个数字,您可以使用regex.search(filename).group(0)(如果您确定它会产生匹配)。如果未找到匹配项,则上述行将产生一个 AttributeError 表示NoneTypehas not attribute group

于 2012-12-23T03:45:56.103 回答
5

所以,你没有留下任何关于这些文件在哪里以及如何获取它们的描述,但我假设你会使用os模块获取文件名。

至于从名称中获取数字,最好使用正则表达式 with 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)

或类似的规定。

于 2012-12-23T03:45:38.030 回答
3

如果只有一个数字:

filter(lambda x: x.isdigit(), filename)
于 2012-12-24T12:53:10.803 回答
0

听到的是我的代码,我用来在文件从谷歌学者下载文件后将论文的发表年份带到文件名的第一个。主要文件通常是这样构造的: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')
于 2020-04-30T18:12:27.113 回答