首页 > 解决方案 > 关于dict.fromkeys,来自文件名的键,文件内的值,使用正则表达式

问题描述

好吧,我正在学习 Python,所以我正在研究一个项目,该项目包括将一些 PDF 文件传递​​给 xlsx 并将它们放置在相应的列中,根据行标题确定行。我的想法是将 PDF 文件转换为 txt 并使用 txt 文件制作字典,其键是文件名的一部分(因为它包含行标题的一部分)和值是我需要的数字。我已经设法转换了 txt 文件,现在我正在处理携带字典的脚本。目前看起来像这样:

import os
import re


p = re.compile(r'\w+\f+')
'''
I'm not entirely sure at the moment how the .compile of regular expressions works, but I know I'm missing something to indicate that what I want is immediately to the right, I'm also not sure if the keywords will be ignored, I just want take out the numbers
'''
m = p.match('Theese are the keywords' or 'That are immediately to the left' or 'The numbers I want')


def IsinDict(txtDir):
    ToData = ()
    if txtDir == "": txtDir = os.getcwd() + "\\"
    for txt in os.listdir(txtDir):
        ToKey = txt[9:21]
        if ToKey == (r"\w+"):
            Data = open(txt, "r")
            for string in Data:
                ToData += m.group()

        Diccionary = dict.fromkeys(ToKey, ToData)
    return Diccionary

txtDir = "Absolute/Path/OfTheText/Files"

IsinDict(txtDir)

欢迎任何贡献,感谢您的关注。

标签: pythonregex

解决方案


推荐阅读