type conversion - ValueError when converting to int lines retrieved via getline.linecache -


this first message here, hope not commit mistake.

i writing python 2.7 script performs comparisons between lines long list of lines provided external input file. of these lines contain numbers, , on perform simple sums after retrieval via getline.linecache.

my problem after number of lines getting error:

valueerror: invalid literal int() base 10 

i understand somehow has fact there problem when try convert lines retrieved integer type, according read each line should retrieved memory database string, , indeed if try print type of values retrieved str. printed problematic values in order understand why failed converted int: @ first included semantic mistakes (i taking wrong lines, containing letters, , of course failed converted int), still error on merely numerical strings. on of numerical strings, tried len(linecache.getline('input', line_n)) see if characters present, found '\n', not give problems when converting str int.

my input file made series of lines, numerical not; here few lines:

1 id3021-a 1 129485768 129485769 2 id2034 102 944709842 944709848 

for examples, line 4 here can retrieved, not converted int. how convert str int without getting errors?

i found solution! adding '0' beginning of string fixes problem (i not know why, problematic lines not empty):

int('0' + linecache.getline('input', line_n)) 

see here: trouble converting string int in django/python


Comments