我做了一個xlrd給json解析腳本,它不會分割變量。它總是返回零...這我使用的劃分變量的代碼是:在Python中劃分值
if not row_values[2]:
key['nr_one'] = 0
else:
key['nr_one'] = int(row_values[2])
if not row_values[4]:
key['nr_two'] = 0
else:
key['nr_two'] = int(row_values[4])
try:
key['perc'] = float(key['nr_two']/key['nr_one']*100)
except ZeroDivisionError:
key['perc'] = 0
我已經在腳本的末尾印有下面的代碼:
print('one')
print(key['nr_one'])
print('two')
print(key['nr_two'])
print('perc')
print(key['perc'])
這將返回:
one
103386547
two
135680054
perc
0.0
所以。我不明白爲什麼分區失敗並返回0?可能有人請幫我格式化計算百分比
太棒了!謝謝! –
很高興它的作品:-) –
當我被允許時,我會接受答案! –