简体   繁体   English

Python 用于从 zip 文件夹中提取文件名包含且不应包含特定模式的文件的正则表达式

[英]Python Regex to extract file where filename contains and also should not contain specific pattern from a zip folder

I want to extract just one specific single file from the zip folder which has the below 3 files.我只想从包含以下 3 个文件的 zip 文件夹中提取一个特定的单个文件。
Basically it should start with 'kpidata_nfile' and should not contain 'fileheader'基本上它应该以“kpidata_nfile”开头,不应该包含“fileheader”

kpidata_nfile_20220919-20220925_fileheader.csv kpidata_nfile_20220919-20220925_fileheader.csv
kpidata_nfile_20220905-20220911.csv kpidata_nfile_20220905-20220911.csv
othername_kpidata_nfile_20220905-20220911.csv othername_kpidata_nfile_20220905-20220911.csv

Below is my code i have tried-以下是我尝试过的代码-

from zipfile import ZipFile
import re
import os
for x in os.listdir('.'):
  if re.match('.*\.(zip)', x):
      with ZipFile(x, 'r') as zip:
          for info in zip.infolist():
              if re.match(r'^kpidata_nfile_', info.filename):
                  zip.extract(info)

Output required - kpidata_nfile_20220905-20220911.csv需要 Output - kpidata_nfile_20220905-20220911.csv

This regex does what you require:此正则表达式可满足您的要求:

^kpidata_nfile(?:(?!fileheader).)*$

See this answer for more about the (?:(?.fileheader).)*$ part.有关(?:(?.fileheader).)*$部分的更多信息,请参阅此答案

You can see the regex working on your example filenames here .您可以在此处查看处理示例文件名的正则表达式。

The regex is not particularly readable, so it might be better to use Python expressions instead of regex.正则表达式的可读性不是特别好,因此最好使用 Python 表达式而不是正则表达式。 Something like:就像是:

fname = info.filename
if fname.startswith('kpidata_nfile') and 'fileheader' not in fname:

暂无
暂无

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

相关问题 使用Python zipfile从ZIP中提取文件名中包含特定字符串的文件 - Extract file that contains specific string on filename from ZIP using Python zipfile 使用 Python3 从特定文件夹中提取并重命名 zip 文件 - Extract and rename zip file from specific folder using Python3 Python:正则表达式从遵循特定模式的文件名中提取字符串 - Python: Regex to extract strings from a file name that follows a specific pattern python 正则表达式从列表中仅提取特定的模式文件名 - python regex to extract only specific pattern file names from a list 从 Python3 中的 zip 存档中提取特定文件夹的内容 - Extract the content of a specific folder from a zip archive in Python3 提取没有文件夹python的zip文件 - extract zip file without folder python Python从FTP下载文件,文件名以特定字符开头 - Python download a file from FTP where the filename starts with specific characters 使用正则表达式从文本文件中提取文件名 - Extract filename from text file using regex 正则表达式:从字符串中提取所有包含包含特定字母的元素 - Regex: Extract all elements that contain a that contain a specific letter from a string python 正则表达式将文本从特定模式打印到另一个模式,但条件是特定字符串应该存在于两者之间 - python regex to print text from a specific pattern to another pattern, but in condition that a specific string should exist in between
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM