파이썬 3에서 열 그룹을 포맷하려고합니다. 지금까지별로 운이 없었습니다. 나는 인쇄 할 기둥을 얻을 수 있었지만 그 기둥들을 일렬로 세울 수는 없었습니다. 다른 정보를 인쇄해야하는 경우 적응할 수 있도록 각 열을 동적으로 만들려고합니다. 그러나 열 서식에서 변수를 사용하려고하기 때문에 키 오류가 계속 발생합니다. 어떻게하면이 문제를 해결할 수 있을지 생각해? 열을 형식화 할 때 키 오류가 발생했습니다. python 3
이
내가 사용하고있는 코드입니다.Indicator :Min :Max ---------------------------------------------------------------------------- Traceback (most recent call last): File "G:/test.py", line 154, in <module> print('{heart:{heart_col_width}}:{motor:{motor_col_width}} {teen:{teen_col_width}{smoke: {smoke_col_width}}{obese:{obese_col_width}'.format(heart, motor, teen, smoke, obese)) KeyError: 'heart'
first_row = ['Indicator',':Min',':Max'] col_width = max(len(word) for word in first_row) +20# padding print ("".join(word.ljust(col_width) for word in first_row)) print('----------------------------------------------------------------------------') heart=['Heart Disease Death Rate (2007)',stateheart_min(),heartdis_min(),stateheart_max(),heartdis_max()] motor=[ 'Motor Vehicle Death Rate (2009)',statemotor_min(),motordeath_min(),statemotor_max(),motordeath_max()] teen=['Teen Birth Rate (2009)',stateteen_min(),teenbirth_min(),stateteen_max(),teenbirth_max()] smoke=['Adult Smoking (2010)',statesmoke_min(),adultsmoke_min(),statesmoke_max(),adultsmoke_max()] obese=['Adult Obesity (2010)',stateobese_min(),adultobese_min(),stateobese_max(),adultobese_max()] heart_col_width = max(len(word) for word in heart) motor_col_width = max(len(word) for word in motor) teen_col_width = max(len(word) for word in teen) smoke_col_width = max(len(word) for word in smoke) obese_col_width = max(len(word) for word in obese) for heart, motor, teen, smoke, obese in zip(heart, motor, teen, smoke, obese): print('{heart:{heart_col_width}}:{motor:{motor_col_width}} {teen:{teen_col_width}{smoke: {smoke_col_width}}{obese:{obese_col_width}'.format(heart, motor, teen, smoke, obese))