我需要更改代码以避免使用SheepName.Startswith在Python中避免使用什么?
我当前的代码能够根据需要进行,并基于从某个字符串开始的文件名进行更改。但是,我只是意识到,在未来几个月中,文件中的某些床单可能有略有不同的名称。 我的代码 -
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
startswith
一次只能与一个字符串一起使用,因此您需要分解测试。startswith
can only be used with one string at a time, so you need to break up the test.