1

I have a list that combines inputs from two sources, that ends up looking like this 'pde_fin' given below. I need to extract the integer values of the elements in the list for further processing. However, the second set of numbers seem to give an error ("invalid literal for int() with base 10: "'1118'").

pde_fin =['2174', '2053', '2080', '2160', '2065', "'1118'", "'1098'", "'2052'", "'2160'", "'2078'", "'2161'", "'2134'", "'2091'", "'2089'", "'2105'", "'2109'", "'2077'", "'2057'"]
for i in pde_fin:
    print(int(i))

2 Answers2

5

The simplest fix is to strip the single quotes:

for i in pde_fin:
    print(int(i.strip("'")))
wjandrea
  • 28,235
  • 9
  • 60
  • 81
1

Use the following code to correct your values:

pde_fin =['2174', '2053', '2080', '2160', '2065', "'1118'", "'1098'", "'2052'", "'2160'", "'2078'", "'2161'", "'2134'", "'2091'", "'2089'", "'2105'", "'2109'", "'2077'", "'2057'"]
for i in pde_fin:
    print(int(i.replace("'",'')))
Code Pope
  • 5,075
  • 8
  • 26
  • 68