在包含 xlsx 文件的文件夹中搜索值

发布于 2025-01-09 03:01:17 字数 1289 浏览 3 评论 0原文

我有这段代码,用户可以输入目录并在所有 xlsx 文件中搜索该特定值的值。

假设我有一个包含几个 xlsx 的文件夹,我想搜索 A-123, 我的问题是它只在少数情况下工作,当我写 A-123 时它不起作用,但在 -123 上工作。

有人可以指导我应该做什么才能使其正常工作吗? 由于我有限的 Python 经验,所有改进都将通过大炉子进行。

from argparse import _CountAction
from itertools import count
from sys import displayhook
import pandas as pd
import os, glob
from openpyxl import Workbook, load_workbook


path = input("Choose directory: ")
number = input("Search for value: ")

print('Searching...')
workbooks = os.listdir(path)
workbooks = [_ for _ in workbooks if not _.startswith('~')]

sheetList3 = []

for workbook in workbooks:
    sheetList2 = []
    ws = load_workbook(os.path.join(path, workbook))
    for sheet_name in (ws.get_sheet_names()):
        sheet = ws.get_sheet_by_name(sheet_name) 
        sheetList1 = [] 
        for value in sheet.values:
            if number in " ".join([str(_).lower() for _ in value if _]):
                tuple = (workbook, sheet_name, value)
                sheetList1.append(tuple)

                sheetList2.append(tuple)
                sheetList3.append(tuple)

for count,value in enumerate(sheetList3):
    df=pd.DataFrame(sheetList3)
df.columns = ["Workbook", "Sheet", "Tag"]   

df=pd.DataFrame(sheetList3)
df.to_excel('file.xlsx' , index = False)

I have this code where the user can input a directory and search for value inside all the xlsx files for that specific value.

Let's say that I have a folder with few xlsx I want to search a A-123,
my issue is that it is only working on few occations, it is not working when i write A-123, but works on -123.

Can someone please guide me on what I should do to get it working correctly.
All improvement will be taken with big hearth due to my limited Python experience.

from argparse import _CountAction
from itertools import count
from sys import displayhook
import pandas as pd
import os, glob
from openpyxl import Workbook, load_workbook


path = input("Choose directory: ")
number = input("Search for value: ")

print('Searching...')
workbooks = os.listdir(path)
workbooks = [_ for _ in workbooks if not _.startswith('~')]

sheetList3 = []

for workbook in workbooks:
    sheetList2 = []
    ws = load_workbook(os.path.join(path, workbook))
    for sheet_name in (ws.get_sheet_names()):
        sheet = ws.get_sheet_by_name(sheet_name) 
        sheetList1 = [] 
        for value in sheet.values:
            if number in " ".join([str(_).lower() for _ in value if _]):
                tuple = (workbook, sheet_name, value)
                sheetList1.append(tuple)

                sheetList2.append(tuple)
                sheetList3.append(tuple)

for count,value in enumerate(sheetList3):
    df=pd.DataFrame(sheetList3)
df.columns = ["Workbook", "Sheet", "Tag"]   

df=pd.DataFrame(sheetList3)
df.to_excel('file.xlsx' , index = False)

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

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

发布评论

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

评论(1

π浅易 2025-01-16 03:01:17

谢谢:) 在输入中添加 .casefold() 解决了这个问题。

Thank you :) adding .casefold() to the input solved that issue.

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