实际应用中,这种形式的数据往往不是我们想要的.
本文要解决的问题是将一个str格式的带有科学计数法数值的二维数组转换为小数形式
输入数据(str)
-999. 0. 0. 0. 0.
0. 0. 0. 0. 0.
5.00e-02 5.20e+01 2.55e+02 2.55e+02 2.55e+02
6.00e-02 6.90e+01 1.95e+02 2.53e+02 2.55e+02
8.00e-02 1.29e+02 2.21e+02 9.00e+01 2.55e+02
1.00e-01 2.55e+02 1.87e+02 1.50e+01 2.55e+02
1.50e-01 2.55e+02 6.90e+01 1.00e+00 2.55e+02
2.00e-01 2.55e+02 1.50e+02 1.50e+01 2.55e+02
0.4 255. 87. 15. 255.
期望输出(str)
-999. 0. 0. 0. 0.
0. 0. 0. 0. 0.
0.05 52. 255. 255. 255.
0.06 69. 195. 253. 255.
0.08 129. 221. 90. 255.
0.1 255. 187. 15. 255.
0.15 255. 69. 1. 255.
0.2 255. 150. 15. 255.
0.4 255. 87. 15. 255.
查找到的方法有几种:
https://docs.python.org/3.4/library/string.html#format-specification-mini-language
> number = 0.0000001
> f"Number: {number}"
'Number: 1e-07'
> f"Number: {number:f}"
'Number: 0.000000'
> f"Number: {number:.10f}"
'Number: 0.0000001000'
只能用于单个浮点数操作
numpy取消科学计数法.
suppress=True 表示取消科学记数法
>import numpy as np
>np.set_printoptions(suppress=True, threshold=np.nan)
>import pandas as pd
>pd.set_option('display.float_format',lambda x : '%.3f' % x)
def table_process(table):
np.set_printoptions(suppress=True)
table_arr = np.fromstring(table, sep='\n')
table_arr = color_arr.reshape((table_arr.size//5, 5))
new_table = str(table_arr)
new_table = re.sub('\\[', '', new_table)
new_table = re.sub('\\]', '', new_table)
return new_table
Python相关视频讲解:python的or运算赋值用法用python编程Excel有没有用处?011_编程到底好玩在哪?查看python文件_输出py文件_cat_运行python文件_shelPython小数和科学计数法互相转换
在Python编程中,我们经常会遇到需要在小数和科学计数法之间进行转换的情况。小数用...
近期请国内外头部出版社可尽快私信博主!——心比天高,仗剑走天涯,保持热爱,奔赴向梦想!低调,谦虚,自律,反思,成长,还算是比较正能量的博主,公益免费传播……内心特别想在AI界做出一些可以推进历史进程影响力的东西(兴趣使然,有点小情怀,也有点使命感呀)…
11-18
猜测python应该是有现成的模块可以解决该问题,不过没找到,所以自己简单写了个函数处理:
def tranform(inputString):
num_value = re.compile('^[0-9.]+([*|x][0-9]+)+[E|e|^][+-]?[0-9]+$')
result = num_value.match(inputString) if resul...
print ("%e" %number)可以将number输出为科学计数法
这里的 number 是要转换的数字或者变量
同理,也可以把科学记数法转化为十进制整数:print ("%d" %number)
同理,二进制,八进制,十六进制也是一样的
当然浮点型的也可以。
print ("%e"%1010)
1.010000e+03