【发布时间】:2019-10-02 16:53:28
【问题描述】:
以下代码读取现有的 MS Excel 电子表格,创建列图,然后将结果导出到另一个 Excel 电子表格。工作表和相关列实际上要大得多,但为了保持上下文简单,我已将地图中的列数配对。
您会注意到,在此过程中,我创建了 2 个 NULL 列并删除了所有重复的行。我正在努力尝试正确的 Try: except: 语句,该语句将验证我没有用创建的 NULL 列覆盖现有列,并验证没有重复的行。我知道我不是,但出于审计目的需要错误日志报告。以下是代码的简单模型,据我所知。我对异常处理还是很陌生,希望能得到您的帮助。提前致谢。
from datetime import datetime
import logging
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s [%(threadName)-12.12s] [%(levelname)-5.5s] %(message)s",
handlers=[logging.StreamHandler()])
os.chdir(r'M:\Loans')
col_map = {'Loan #' : 'LoanNo',
'Last Name' : 'LastName',
'Purchase Price' : 'PurchasePrice',
'Loan Amt' : 'LoanAmt',
'Property Address' : 'PropertyAddress',
'City' : 'City',
'State' : 'State',
'Zip Code' : 'ZipCode',
'Interest Rate' : 'InterestRate',
'UPBCurrent' : 'UPBCurrent',
'NextDueDateAtPurchase' : 'NextDueDateAtPurchase',
'CurrentAdvanceRate': 'CurrentAdvanceRate',
'Comments' : 'Comments',
'CurrentAdvanceAmount': 'CurrentAdvanceAmount',
'SecondRoundCurrentAdvanceRate' : 'SecRoundCurrentAdvRate',
'SecondRoundCurrentAvanceAmount' : 'SecRoundCurrentAdvAmount',
}
for f in os.listdir():
logging.info('Reading in file {}'.format(f))
df=pd.read_excel('M:\Loans\Loan Blotter XYZ OLD.xlsx')
df['UPBCurrent'] = None
df['NextDueDateAtPurchase'] = None
df = df[col_map.keys()]
df.drop_duplicates(inplace=True)
df.columns = [col_map[col] for col in df.columns]
df['Channel'] = 'Whole Loans'
df['DateCreated'] = datetime.today().date()
df.to_excel(r'M:\Err Log.xlsx', index=False)
【问题讨论】:
标签: python excel pandas exception