ValueError: invalid literal for int() with base 10: '405,000'
the code is as follows :
for d in csv.DictReader(open(c_name + '.csv'), delimiter=','):
global d_value
d_value = int(d['Debt Value'])
405,000
不是有效的int
。您要么需要删除该逗号:
d_value = int(d['Debt Value'].replace(',', ''))
import locale
# You might need to set a locale
# locale.setlocale(locale.LC_ALL, '')
d_value = locale.atoi(d['Debt Value'])
看起来您的值中有一个逗号,这就是您看到错误的原因。您可能希望通过执行以下操作来清理您的号码:
d_value = int(d['Debt Value'].replace(",", ""))
当您使用它时,最好在完成文件后关闭它们。您可以使用with 语句来确保它会在您的块之后关闭。
with open(c_name + ".csv") as csvfile:
for d in csv.DictReader(csvfile, delimiter=','):
global d_value
d_value = int(d['Debt Value'].replace(",", ""))