Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/328.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 ValueError:无法将字符串转换为浮点:';Abs传感器值';and TypeError:';的操作数类型不受支持;浮动';和';str';_Python_Excel - Fatal编程技术网

Python ValueError:无法将字符串转换为浮点:';Abs传感器值';and TypeError:';的操作数类型不受支持;浮动';和';str';

Python ValueError:无法将字符串转换为浮点:';Abs传感器值';and TypeError:';的操作数类型不受支持;浮动';和';str';,python,excel,Python,Excel,我正在为我的论文创建一个数据处理。当我打印str1值时,会出现2.802176590012075,但我无法将其转换为float或在操作中使用它。任何有助于理解这一问题的人都将不胜感激。excel文件的值类似于2802176590012075 import openpyxl # Give the location of the file path = "C:\\Users\\Acer\\.spyder-py3\\output_sens_1660[K]_1.7[atm]_phi=0.5_

我正在为我的论文创建一个数据处理。当我打印str1值时,会出现2.802176590012075,但我无法将其转换为float或在操作中使用它。任何有助于理解这一问题的人都将不胜感激。excel文件的值类似于2802176590012075

import openpyxl

# Give the location of the file
path = "C:\\Users\\Acer\\.spyder-py3\\output_sens_1660[K]_1.7[atm]_phi=0.5_SORTED_descending_DATA_FILE_Perturbation=0.5_.xlsx"
  
# to open the workbook 
# workbook object is created
wb_obj = openpyxl.load_workbook(path)
sheet_obj = wb_obj.active

e=0.55

wb_obj.save(filename="teste.xlsx")
i=0
L=int(len(sheet_obj['D']))
erro=[]*L

for cell in sheet_obj['D']:
    i=i+1
    str1=cell.value
    # changing ',' to '.' 
    def Replace(str1):
        maketrans = str1.maketrans
        final = str1[i].translate(maketrans(',.', '.,', ' '))
        return final.replace(',', ", ")
    x=float(str1)
    #desired operation
    erro[i]=1-(e-str1)/e
最快的:

i=1
将起始索引从0(第一行:标题单元格)更改为1(第二行:标题后的第一个数据单元格)。 但是它会影响
erro
变量:列
'D'
len
gth是内容的单元格数,但数据单元格数是减去1。但这意味着你的指数会移动1,这是未来误差的来源

因此,我建议您跳过循环中的第一行:

活页_obj['D']中单元格的
:
i=i+1
如果i==1:
继续#跳过标题
str1=单元格的值
我经常需要使用Excel文件,为了避免混淆,我更喜欢使用冗长的名称,并区分索引和数字:

column_cells=sheet_obj['D']
列\u单元格\u计数=len(列\u单元格)
标题行编号=1
开始扫描线编号=1
对于范围内的当前索引(列单元格计数):
当前行数=当前索引+1
如果当前行号=标题行号:
持续
其他:
...
最后,我不认为您的
erro
变量必须是您想要的值:

>>L=4
>>>[]*L#将空列表乘以整数
[]给出了一个空列表

如果您想要的是一个列表列表,请不要使用
[[]]*L
,而是将
[[]]用于范围内(L)]
,因为。

您的错误消息说您正在尝试将字符串
'Abs sens value'
转换为
浮点值。
'Abs sens value'
是列标题吗?是的!现在我是阿什蒙德,我该如何跳过头球?