1

Good Day everybody! Is there away to solve this problem ValueError: invalid literal for int() with base 10: '\r' im getting this problem after reading the data from mysql database, i have seen related post same problem , but its not working the solution with me such like here and here and here the data can be read successfully from database but the program stop into this line in the code indices = list(int(x) for x in data[-2:]) : the code working fine when i set the value direct but the problem happen when i read data from database. suppose this data is store in database:

data=3669173122946610524661732905264584239464321015293887264584378370174699627442417573669172466172466172938871746993783703210153210152394642394641864073669176459629027117604963001838526185423176139908966

data=str(data)
def split(data):
    indices = list(int(x) for x in data[-2:]) 
    data = data[:-2]
    rv = []
    for i in indices[::-1]:
        rv.append(data[-i:])
        data=data[:-i]
    rv.append(data)
    return rv[::-1]
d1, d2, d3 = split(data)

i'm adding the reading data from database

    import mysql.connector
from mysql.connector import Error

try:
   mySQLconnection1 = mysql.connector.connect(host='localhost',
                             database='vcn',
                             user='vcn',
                             password='vcn')
   sql_select_Query = "select * from pid where id =56"
   cursor = mySQLconnection1 .cursor()
   cursor.execute(sql_select_Query)
   records = cursor.fetchall()

   for row in records:

       data= row[1]

       print(data)

   cursor.close()

except Error as e :
    print ("Error while connecting to MySQL", e)
finally:
    #closing database connection.
    if(mySQLconnection1 .is_connected()):
        mySQLconnection1.close()
        print("MySQL connection is closed")

any suggestion!! thanks all

honey
  • 43
  • 5
  • 2
    You say your data is coming in from sql, apparently it has an extra new line at the end. Use `.strip()` prior to parsing it to int – G. Anderson Jan 31 '20 at 19:32
  • 2
    @G.Anderson I think db should not give `LF` or `CRLF` by default there should be some configuration problem or at data write `\n` and `\r` should be replaced by empty string – Mayur Jan 31 '20 at 19:34
  • sorry, where i should mention .strip() in the cod? – honey Jan 31 '20 at 19:35
  • @Mayur, im reading the data successfully from database but the problem happen when i pass the data into this code showing me that message – honey Jan 31 '20 at 19:36

1 Answers1

2

It seems you are getting CRLF from db just use strip function

data=3669173122946610524661732905264584239464321015293887264584378370174699627442417573669172466172466172938871746993783703210153210152394642394641864073669176459629027117604963001838526185423176139908966

data=str(data)
def split(data):
    data = data.strip()
    indices = list(int(x) for x in data[-2:]) 
    data = data[:-2]
    rv = []
    for i in indices[::-1]:
        rv.append(data[-i:])
        data=data[:-i]
    rv.append(data)
    return rv[::-1]
d1, d2, d3 = split(data)
Mayur
  • 2,583
  • 16
  • 28