regex 在python中从文件名提取日期,而不是固定名称

ipakzgxi  于 2023-01-31  发布在  Python
关注(0)|答案(1)|浏览(117)

我需要在python代码中从文件名中获取日期。我找到了很多解决方案,但是从固定的名称和日期。但是我不知道文件的名称将是什么,日期正在更改。如何做到这一点?
我有一个代码是已知的文件名(当前日期),文件名为micro20230125.txt

import re

import os
from datetime import datetime

header = """#SANR0000013003;*;#CNR0010;*;#RINVAL-777.0;*;"""

current_timestamp = datetime.today().strftime('%Y%m%d')
input_file = "micro" + current_timestamp + ".txt"
output_file = os.path.splitext(input_file)[0] + ".zrxp"

with open(input_file, "r") as f:
    first_line = f.readline().strip('\n')

    text = re.search('(\d{6})', first_line).group(1)


text = header + "\n" + text + "\n"

with open(output_file, "w") as f:
    f.write(text)
    print(text)
`

但是我不需要当前日期。2我会得到一些随机日期的文件,那么我怎样才能从文件名中提取未知的日期呢?3怎样改变这个变量current_timestamp?
我试着用正则表达式但我搞砸了

编辑:不同代码,类似问题:

我在处理这些代码时意识到:python不知道name中的数字代表什么,所以为什么要把它们当作日期来处理呢?这些只是数字。事实上,我需要这些数字和完整的文件名一样长。所以我用了不同的代码。

import re
import os

def get_numbers_from_filename(filename):
    return re.search(r'\d+', filename).group(0) #returns only numbers

for filename in os.listdir("my path"):
   print (get_numbers_from_filename(filename))

def get_numbers_from_filename(filename):
   return re.search(r"(.)+", filename).group(0) #returns all name


for filename in os.listdir("my path"):
       print(get_numbers_from_filename(filename))

文件为:微20230104.txt
结果是:
result
现在,我想使用这个结果,不想打印它。无论我如何得到它,都会返回错误。

import re
import os

def get_numbers_from_filename(filename):
    return re.search(r"(.)+", filename).group(0)

for filename in os.listdir("my path"):
    print(get_numbers_from_filename(filename))
    m = get_numbers_from_filename(filename)
    output_file = os.path.splitext(m)[0] + ".zrxp"

       with open(m, "r") as f:
           first_line = f.readline().strip('\n')

           text = re.search('(\d{6})', first_line).group(1)

       text = header + "\n" + text + "\n"

       with open(output_file, "w") as f:
           f.write(text)
           print(text)

但它显示错误error:there is no such file
怎么办?我做错了什么?

yhived7q

yhived7q1#

那么,如果所有的文件都有格式'micro[YearMonthDay]. txt',你可以尝试这个解决方案:

import os
from datetime import datetime

header = """#SANR0000013003;*;#CNR0010;*;#RINVAL-777.0;*;"""

#Change the variable folder_path for your actual directory path.
folder_path = "\\path_files\\"
filenames = []
# Iterate directory
for path in os.listdir(folder_path):
    # check if current path is a file
    if os.path.isfile(os.path.join(folder_path, path)):
        filenames.append(path)

dates = []
for filename in filenames:
    # First solution:
    filename = filename.replace('micro', '')
    filename = filename.replace('.txt', '')
    date = datetime.strptime(filename, "%Y%m%d")

    # Second solution:
    # date = datetime.strptime(filename, "micro%Y%m%d.txt")

    dates.append(date)

for date in dates:
    print(date.strftime("%Y/%m/%d"))

    with open(f'.\\micro{date.strftime("%Y/%m/%d")}.txt', "r") as f:
        first_line = f.readline().strip('\n')

    text = re.search('(\d{6})', first_line).group(1)

    text = header + "\n" + text + "\n"

    with open(output_file, "w") as f:
        f.write(text)
        print(text)

使用你喜欢的解决方案并注解另一个。

测试:

1.测试文本文件

1.代码

1.结果

我希望我能帮上忙!:D

相关问题