简体   繁体   English

如何删除小数点

[英]how to remove the decimal point

I use numpy.loadtxt() to read some numbers (integers) from files, however, when I print it, it displayed by scientific notation. 我使用numpy.loadtxt()从文件中读取一些数字(整数),但是,当我打印它时,它以科学计数法显示。 I use suppress=True, in the end , the decimal disappear. 我使用抑制=真,最后,小数点消失了。 But there is still a decimal point. 但是仍然有一个小数点。 How to remove? 如何删除?

在此处输入图片说明

There is an optional argument of numpy.loadtxt() called dtype which allows you to specify the datatype of the array. 有一个可选的参数numpy.loadtxt()称为dtype ,它允许你指定数组的数据类型。 If you don't provide any argument, the default is float, which is what you are seeing. 如果不提供任何参数,则默认值为float,这就是您所看到的。

Therefore, you can do something like: 因此,您可以执行以下操作:

import numpy as np
data = np.loadtxt("filename.txt", dtype=np.int16)

You can convert your numpy type to int before printing. 您可以在打印之前将numpy类型转换为int

# Create example data
example = np.random.rand(10, 2)*10

# Print as integer
print(example.astype(int))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM