草庐IT

GapPoints

全部标签

python - 如何在 Python 中从文件名中提取数字?

我只需要从文件名中提取数字,例如:GapPoints1.shpGapPoints23.shpGapPoints109.shp如何使用Python从这些文件中提取数字?我需要将其合并到for循环中。 最佳答案 你可以使用正则表达式:regex=re.compile(r'\d+')然后获取匹配的字符串:regex.findall(filename)这将返回包含数字的字符串列表。如果你真的想要整数,你可以使用int:[int(x)forxinregex.findall(filename)]如果每个文件名中只有1个数字,您可以使用regex