soupe soupe - 6 months ago 21
SQL Question

Django save scientific number

I'm trying to find a way to format a scientific number and save it into database. My number is read from a csv file.

model.py

class Doc(models.Model):
....
big=models.BigIntegerField(null=True)


views.py

for line in csvfile:
data = Doc()
data.big=line['big_number']
data.save()


This following code generate this error:


invalid literal for int() with base 10: '2,02161E+12'


2,02E+12 is the value on csv file when it's read on standart format and 2021610000000 for the number format. I want to store the "2021610000000".
Is there a way to force reading the csv column as number ?

Answer

Your data is malformed. While the int('2,02161E+12') and float('2,02161E+12') would indeed produce that error float('2.02161E+12') would not. So the solution is

int( float('2,02161E+12'.replace(',','.)) )
Comments