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,588
  • 5
  • 25
  • 48
Mudits
  • 1,513
  • 3
  • 20
  • 37

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
  • 289,723
  • 53
  • 439
  • 496
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,628
  • 1
  • 21
  • 29