使用python将不同类型的浮点数格式化为原始格式

使用python将不同类型的浮点数格式化为原始格式,python,formatting,Python,Formatting,我正在使用python/cx_Oracle选择Oracle数据,并使用相同的字段类型插入到Impala中 我在Oracle表中有数字字段,它的样本数据就是这样的 0.1428571428571428571428571428571428571429 0 0.2111 2.1 0.04 sql4Impala = "insert into test01.ornek_2 values (%f, '%s', '%s', '%s', '%s')" % (row[0], row[1], row[2], ro

我正在使用python/cx_Oracle选择Oracle数据,并使用相同的字段类型插入到Impala中

我在Oracle表中有数字字段,它的样本数据就是这样的

0.1428571428571428571428571428571428571429
0
0.2111
2.1
0.04
sql4Impala = "insert into test01.ornek_2 values (%f, '%s', '%s', '%s', '%s')" % (row[0], row[1], row[2], row [3], row[4])
因此,我尝试创建sql字符串,以便像这样将数据插入到Impala中

0.1428571428571428571428571428571428571429
0
0.2111
2.1
0.04
sql4Impala = "insert into test01.ornek_2 values (%f, '%s', '%s', '%s', '%s')" % (row[0], row[1], row[2], row [3], row[4])
我的问题是从这里开始,我不知道从oracle端选择了什么样的实数。因此无法将数字格式化为原始格式

比如说

0.0344827586207 it's original data
0.034483        automatically trimed out with %f
那么,当我不知道原始数据的格式时,如何在sql4Impala中格式化实数呢


谢谢

如果您直接获取数据,您将返回浮点数,这些浮点数的精度与存储在数据库中的Oracle数字不同。要解决这个问题,请以字符串形式检索数据,然后改用decimal.decimal(value)。像这样:

connection = cx_Oracle.connect("user/pw@tns")
cursor = connection.cursor()
cursor.execute("select to_char(some_number) from some_table")
for rawNumber, in cursor:
    number = decimal.Decimal(rawNumber)