1

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'])
aldeb
  • 6,014
  • 3
  • 23
  • 47
Mudits
  • 1,321
  • 2
  • 15
  • 35

2 Answers2

3

405,000 isn't a valid int. You either need to remove that comma:

d_value = int(d['Debt Value'].replace(',', ''))

Or use the locale module:

import locale

# You might need to set a locale
# locale.setlocale(locale.LC_ALL, '')

d_value = locale.atoi(d['Debt Value'])
Community
  • 1
  • 1
Blender
  • 275,078
  • 51
  • 420
  • 480
3

It looks like you have a comma in your value, which is why you're seeing the error. You may want to sanitize your numbers by doing something like this:

    d_value = int(d['Debt Value'].replace(",", ""))

While you're at it, it's a good idea to close your files when you're done with them. You can use a with statement to make sure it will close after your block.

with open(c_name + ".csv") as csvfile:
    for d in csv.DictReader(csvfile, delimiter=','):
        global d_value
        d_value = int(d['Debt Value'].replace(",", ""))
Kevin London
  • 4,444
  • 1
  • 19
  • 26