在包含 xlsx 文件的文件夹中搜索值
我有这段代码,用户可以输入目录并在所有 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
谢谢:) 在输入中添加 .casefold() 解决了这个问题。
Thank you :) adding .casefold() to the input solved that issue.