python - 关于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)
欢迎任何贡献,感谢您的关注。
解决方案
推荐阅读
- javascript - .React 中的拆分方法
- javascript - 使用 Vuex 设置值
- c# - 当您拥有 IHostedService 时,拥有 IHostApplicationLifetime 有什么意义
- python - 我如何获取/定义我的图形的输入输出名称或以正确的形式冻结它
- c# - Linq FirstOrDefault(
).属性与位置( ).Select(a => Attribute).FirstOrDefault() - ios - 电话号码验证失败 - Flutter(仅限 iOS)
- wpf - WPF SharedSizeGroup 没有正确调整
- java - 在 Java 中使用掩码图像的十六进制进行地图冲突的问题
- oracle18c - Oracle 18c - 创建实体化视图时出错 - ORA-00933
- servlets - 使用 LTI(使用 oauth2)如何启动对 canvas LMS API 的查询?