我使用在各種功能的記錄模塊,並且它踢了,看起來像這樣的錯誤堆棧:啓動KeyErrors的Python日誌記錄模塊?
KeyError: 'level' x
Traceback (most recent call last): x
File "/usr/lib64/python2.6/logging/__init__.py", line 776, in emit x
msg = self.format(record) x
File "/usr/lib64/python2.6/logging/__init__.py", line 654, in format x
return fmt.format(record) x
File "/usr/lib64/python2.6/logging/__init__.py", line 439, in format x
s = self._fmt % record.__dict__ x
KeyError: 'level' x
Traceback (most recent call last): x
File "/usr/lib64/python2.6/logging/__init__.py", line 776, in emit x
msg = self.format(record) x
File "/usr/lib64/python2.6/logging/__init__.py", line 654, in format x
return fmt.format(record) x
File "/usr/lib64/python2.6/logging/__init__.py", line 439, in format x
s = self._fmt % record.__dict__ x
KeyError: 'level' x
Traceback (most recent call last): x
File "/usr/lib64/python2.6/logging/__init__.py", line 776, in emit x
msg = self.format(record) x
File "/usr/lib64/python2.6/logging/__init__.py", line 654, in format x
return fmt.format(record) x
File "/usr/lib64/python2.6/logging/__init__.py", line 439, in format x
s = self._fmt % record.__dict__ x
KeyError: 'level' x
Traceback (most recent call last): x
File "/usr/lib64/python2.6/logging/__init__.py", line 776, in emit x
msg = self.format(record) x
File "/usr/lib64/python2.6/logging/__init__.py", line 654, in format x
return fmt.format(record) x
File "/usr/lib64/python2.6/logging/__init__.py", line 439, in format x
s = self._fmt % record.__dict__ x
KeyError: 'level'
我竭力要找出原因。這裏是我的代碼:
from csv import reader, writer
import logging
from sys import argv
from types import IntType, FloatType
def main():
logging.basicConfig(level=logging.INFO, format='[%(level)s] - %(message)s')
# logging.disable(logging.CRITICAL)
# Files
infile = argv[1]
header_file = argv[2]
transact_file = argv[3]
start_process(infile)
def start_process(infile):
"""Create a csv reader and parse it into two lists."""
with open(infile, 'r') as inf:
logging.info('Infile name: {0}'.format(inf))
csv_reader = reader(inf, quotechar='"')
parse_headers(csv_reader)
def parse_headers(reader_obj):
"""Separate header files ("H", "S") from transaction files."""
headers = []
transactions = []
for row in reader_obj:
row_type = row[0]
logging.info('Row type is: {0}'.format(row_type))
if row_type == 'H':
logging.info('Row added to header list.')
headers.append(row)
if row_type == 'S':
if row not in headers:
headers.append(row)
else:
logging.info('Row added to transaction list.')
transactions.append(row)
logging.info('Header list contains: {0}'.format('\n'.join([str(header) for header
in headers])))
logging.info('Transaction list contains: {0}'.format(
'\n'.join([str(trans) for trans in transactions])))
recon_totals(headers, transactions)
def recon_totals(header_list, transact_list):
"""Reconcile the check total amount and document count."""
# Client totals
client_doc_count = int(header_list[0][6])
client_check_tot = float(header_list[0][7])
# Double check variable typing for reconciliation totals.
logging.info('Document count is: {0}'.format(client_doc_count))
doc_var_type = type(client_doc_count)
assert doc_var_type is IntType, 'Doc Count is not an integer: {0}'.format(
doc_var_type)
logging.info('Check Total is: {0}'.format(client_check_tot))
check_var_type = type(client_check_tot)
assert check_var_type is FloatType, 'Check tot is not a float: {0}'.format(
check_var_type)
# RRD totals
rrd_doc_count = 0
rrd_check_tot = 0.0
for transact in transact_list:
row_type = transact[0]
logging.info('Transaction type is: {0}'.format(row_type))
if row_type == 'P':
rrd_doc_count += 1
trans_chk_amt = float(transact[12])
trans_chk_type = type(trans_chk_amt)
assert trans_chk_type is FloatType, 'Transaction Check Total is '\
'not a float: {0}'.format(
trans_chk_type)
rrd_check_tot += trans_chk_amt
# Reconcile totals
if (client_doc_count, client_check_tot) == (rrd_doc_count, rrd_check_tot):
write_files()
else:
raise ValueError('Recon totals do not match! Client: {0} {1} '
'RRD {2} {3}'.format(client_doc_count,
client_check_tot,
rrd_doc_count,
rrd_check_tot))
def write_files(header_file, transact_file, header_data, transact_data):
pass
if __name__ == '__main__':
main()
作爲一個側面說明,它是不好的做法,在鏈調用一個函數從另一個像我在做什麼?我應該把它作爲單獨的過程來完成嗎?我打算創建一個能夠處理這一切的課程,但後來我決定沒有真正的好處。
是的,總DERP一刻對我來說:/ – flybonzai