使用 python 在多个文本文件中搜索字符串

发布于 2025-01-20 01:46:45 字数 1620 浏览 2 评论 0原文

我正在运行MacOS,如果这有帮助的话,

我计划使用通用脚本创建一个数据库来管理条目。

其中一个选项是搜索条目(也称为人)。

这是每个条目的第一个字符串的模板:

n: 姓名 fn: 家庭名称 a: dd.mm.yyyy eid: XXXXXXXXX e: 活动/非活动

我需要一个脚本来搜索文件夹中的每个文件(存储条目的位置,我的是“ /Database ”) 我制作了一个简单的脚本

,使用关键字询问您正在寻找的人

Name = input("[Name] ")

FamilyName = input("[Family Name] ")

DOB = input("[Date of Birth] ")

EmployeeID = input("[Employee ID] ")

Email = input("[Email] ")

Status = input("[Status] ")


output = " n: " + Name + " fn: " + FamilyName + " a: " + DOB + " eid: " + EmployeeID + " e: " + Status

我还使用了另一篇 StackOverflow 帖子中的脚本 点击此处: 我对其进行了一些修改以适应我的变量。

dir = "/Users/user/Project/Database"
directory = os.listdir(dir)

searchstring = output

for fname in directory:
   if os.path.isfile(dir + os.sep + fname):
     # Full path
     f = open(dir + os.sep + fname, 'r')

     if searchstring in f.read():
        print('found string in file %s' % fname)
     else:
        print('string not found')
     f.close()

我运行该文件,它给了我这个错误:

回溯(最近一次调用最后一次): 文件“/Users/user/project/new.py”,第 23 行,位于 如果在 f.read() 中搜索字符串: 文件“/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/codecs.py”,第 322 行,解码 (结果,消耗)= self._buffer_decode(数据,self.errors,最终) UnicodeDecodeError:“utf-8”编解码器无法解码位置 3131 中的字节 0x80:起始字节无效

我希望这不是太大的问题,

欢迎提出有关如何重新设计我的程序的建议

我我想我可能想重新设计第一行的内容,以便搜索符合搜索要求的文件可以更容易,或者只是完全删除第一行的内容,也许可以使用元数据?我不知道

I am running MacOS if that helps this at all

I am planning to make a database, with a general script, to manage the entries.

One of the options is to search for an entry (aka a person).

here is a template of what the first string of each entry will be:

n: Name fn: FamilyName a: dd.mm.yyyy eid: XXXXXXXXX e: Active/Inactive

I need a script that'll search every file in the folder (where the entries are stored, mine is " /Database " ) for that strings

I made a simple script that asks you for the person you are looking for using the keywords

Name = input("[Name] ")

FamilyName = input("[Family Name] ")

DOB = input("[Date of Birth] ")

EmployeeID = input("[Employee ID] ")

Email = input("[Email] ")

Status = input("[Status] ")


output = " n: " + Name + " fn: " + FamilyName + " a: " + DOB + " eid: " + EmployeeID + " e: " + Status

I'm also using a script from another StackOverflow post click here:
I modified it a little to fit my variables.

dir = "/Users/user/Project/Database"
directory = os.listdir(dir)

searchstring = output

for fname in directory:
   if os.path.isfile(dir + os.sep + fname):
     # Full path
     f = open(dir + os.sep + fname, 'r')

     if searchstring in f.read():
        print('found string in file %s' % fname)
     else:
        print('string not found')
     f.close()

I run the file and it gives me this error:

Traceback (most recent call last):
File "/Users/user/project/new.py", line 23, in
if searchstring in f.read():
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/codecs.py", line 322, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x80 in position 3131: invalid start byte

I hope this isn't too much of a problem,

Suggestions on how to redesign my program are welcome

I think I might want to redesign the first line thing, so that searching for files matching the search requirements can be easier, or just completely remove the first line thing, and maybe use metadata? I don't know

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

念﹏祤嫣 2025-01-27 01:46:45

我想我找到了答案,我将使用 TinyDB github 链接,它简化了整个过程,

再见!

I think I found my answer, I'm going to use TinyDB github link, it simplifies this whole process,

Bye!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文