我需要更改代码以避免使用SheepName.Startswith在Python中避免使用什么?

发布于 2025-01-26 13:34:38 字数 1544 浏览 2 评论 0原文

我当前的代码能够根据需要进行,并基于从某个字符串开始的文件名进行更改。但是,我只是意识到,在未来几个月中,文件中的某些床单可能有略有不同的名称。 我的代码 -

import pandas as pd
from openpyxl import load_workbook
import os
cols_to_drop =  ['PSI ID','PSIvet Region','PSIvet region num']              
column_name_update_map = {'Account name': 'Company Name','Billing address':'Address','Billing city':'City'} 

for file in os.listdir("C:/Users/hhh/Desktop/gu/python/PartMatching"):
    if file.startswith("PSI"):
        dfs = pd.read_excel(file, sheet_name=None)
        output = dict()
        for ws, df in dfs.items():
            if ws in ["Added"]:
                continue
            if ws in ["New Members 03.22", "PVCC"]:   #sheets to avoid
                temp = df
                temp['Status'] = "Active" if ws == "All Members" else "Cancelled"
            #drop unneeded columns
            temp = df.drop(cols_to_drop, errors="ignore", axis=1)
            #rename columns
            temp = temp.rename(columns=column_name_update_map)
            #drop empty columns
            temp = temp.dropna(how="all", axis=1)
            temp['Partner'] = "PSI"
            output[ws] = temp
        writer = pd.ExcelWriter(f'{file.replace(".xlsx","")} (updated headers).xlsx')
        for ws, df in output.items():
            df.to_excel(writer, index=None, sheet_name=ws)
        writer.save()
        writer.close()

我的目标是使我的当前代码避免使用其名称以“新成员”开头的表格。但是,正如您在我的代码中看到的那样,我必须具体提及新成员03.22。下个月的这张表将被命名为新成员04.22,因此与我的代码不兼容以在计划的任务上运行。我尝试了[“新成员03.22”,“ PVCC”]中的ws.startswith:但是什么也没有发生。

My current code is able to do as desired and make changes in all sheets based on filename that starts with a certain string. But, i just realized that some of the sheets within the file may have slightly different names in the months going forward.
My code-

import pandas as pd
from openpyxl import load_workbook
import os
cols_to_drop =  ['PSI ID','PSIvet Region','PSIvet region num']              
column_name_update_map = {'Account name': 'Company Name','Billing address':'Address','Billing city':'City'} 

for file in os.listdir("C:/Users/hhh/Desktop/gu/python/PartMatching"):
    if file.startswith("PSI"):
        dfs = pd.read_excel(file, sheet_name=None)
        output = dict()
        for ws, df in dfs.items():
            if ws in ["Added"]:
                continue
            if ws in ["New Members 03.22", "PVCC"]:   #sheets to avoid
                temp = df
                temp['Status'] = "Active" if ws == "All Members" else "Cancelled"
            #drop unneeded columns
            temp = df.drop(cols_to_drop, errors="ignore", axis=1)
            #rename columns
            temp = temp.rename(columns=column_name_update_map)
            #drop empty columns
            temp = temp.dropna(how="all", axis=1)
            temp['Partner'] = "PSI"
            output[ws] = temp
        writer = pd.ExcelWriter(f'{file.replace(".xlsx","")} (updated headers).xlsx')
        for ws, df in output.items():
            df.to_excel(writer, index=None, sheet_name=ws)
        writer.save()
        writer.close()

My goal is to make my current code avoid the sheet whose name starts with "New Members". But as you can see in my code I have to specifically mention New Members 03.22. This sheet next month will be named New Members 04.22 and so wont be compatible with my code to run on a scheduled task. I tried if ws.startswith in ["New Members 03.22", "PVCC"]: but nothing happened.

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

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

发布评论

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

评论(1

南薇 2025-02-02 13:34:38

startswith一次只能与一个字符串一起使用,因此您需要分解测试。

if any(ws.startswith(x) for x in ["New Members", "PVCC"]):

startswith can only be used with one string at a time, so you need to break up the test.

if any(ws.startswith(x) for x in ["New Members", "PVCC"]):
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文