Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 列表项显示不应存在的小数点后位置_Python_Floating Point - Fatal编程技术网

Python 列表项显示不应存在的小数点后位置

Python 列表项显示不应存在的小数点后位置,python,floating-point,Python,Floating Point,我正在从Excel列中读取数据,并使用自编函数将其保存在numpy数组中。我只有一个问题 以下是函数: import numpy as np import xlrd import os # Data Path input_data_path = os.path.join(os.path.dirname(__file__), "..", r"Input.xlsx") def read_excel_file_column(filepath, sheetpage, column=0):

我正在从Excel列中读取数据,并使用自编函数将其保存在numpy数组中。我只有一个问题

以下是函数:

import numpy as np
import xlrd
import os

# Data Path
input_data_path = os.path.join(os.path.dirname(__file__), "..", r"Input.xlsx")


def read_excel_file_column(filepath, sheetpage, column=0):
    """Gives back a Excel-column as array"""

    # file
    book = xlrd.open_workbook(filepath, encoding_override="utf-8")

    # sheet
    sheet = book.sheet_by_index(sheetpage)

    # numpy-Array
    data = np.array([sheet.cell(i, column).value for i in range(sheet.nrows)])
    return data
然后我调用函数,从excel文件中读取一列,第一行有一个字符串,之后是49个浮点值,然后打印出来

以下是输出:

['CTRobot' '3.9000000000000004' '0.8999999999999999' '4.199999999999999'
 '6.75' '5.25' '4.35' '6.8999999999999995' '10.35' '3.3000000000000003'
 '3.3000000000000003' '38.25' '6.1499999999999995' '7.199999999999999'
 '4.050000000000001' '2.4000000000000004' '2.4000000000000004' '1.0'
 ...]
我不明白为什么有这么多小数点后的位置

我检查了我的Excel表格,删除了所有的食品信息,以确保它不仅显示了四舍五入的值或其他内容

另一个非常有趣的是,它与列“E”的效果非常好,基本上是相同的:

函数调用:

time_human = read_excel_file_column(input_data_path, 2, column=4)
输出:

['CTHuman' '2.6' '0.6' '2.8' '4.5' '3.5' '2.9' '4.6' '6.9' '2.2' '2.2'
 '25.5' '4.1' '4.8' '2.7' '1.6' '1.6' '9.2' ...]

我使用的是Windows 10和python 3.7.5。

如果要删除小数,可以对列表中的每一项使用try-except语句,将它们转换为浮点数,并在可能的情况下对其进行舍入。是否检查了从
sheet.cell(I,column)中获得的原始值之间的差异.两列的值
?这似乎很关键。有一些事情超出了正常的浮点行为。例如,与3.9最接近的IEEE 754 64位二进制浮点是3.899999999911182158029987476766109466552734375。下一个数字是3.900000000000003552713678800500929355621337890625,与3.9不太接近。这两个专栏的创建方式有什么不同吗?所有人:非常感谢你的快速回答@martineau:没有,你的意思是把它们打印出来,看看有什么不同?帕特里西斯:不,我已经重做了,并注意到它们是平等创造的
['CTHuman' '2.6' '0.6' '2.8' '4.5' '3.5' '2.9' '4.6' '6.9' '2.2' '2.2'
 '25.5' '4.1' '4.8' '2.7' '1.6' '1.6' '9.2' ...]