是否有函数可以从 Excel 中获取的数据中去除撇号?
Is there a function that can strip the apostrophe from the data which has been taken from Excel?
我正在从 Excel 中提取数据作为多维数组。当我尝试遍历数组以获取每个值时,获得的列表中很少有值包含撇号,而其他一些值则不包含。有什么办法可以纠正这个问题吗?
我试过使用 strip 函数,但效果不佳。
请帮忙。
我希望输出是
(2.56942078E+00, -8.59741137E-05, 4.19484589e-08, -1.00177799e-11, 1.22833691e-15, 29217.5791, 4.78433864)
但我最终得到了
('2.56942078E+00', '-8.59741137E-05', 4.19484589e-08, -1.00177799e-11, 1.22833691e-15, 29217.5791, 4.78433864)
撇号表明该项是字符串而不是浮点数。 '
只是字符串的表示。要将其用作浮点数,只需转换类型即可。
array = ('2.56942078E+00', '-8.59741137E-05', 4.19484589e-08, -1.00177799e-11, 1.22833691e-15, 29217.5791, 4.78433864)
for item in array:
print(type(item))
<class 'str'>
<class 'str'>
<class 'float'>
<class 'float'>
<class 'float'>
<class 'float'>
<class 'float'>
看看前两项是字符串而不是浮点数。
现在只需将项目转换为浮动,然后再对其进行其他操作:
for item in array:
item = float(item)
print(type(item))
<class 'float'>
<class 'float'>
<class 'float'>
<class 'float'>
<class 'float'>
<class 'float'>
<class 'float'>
我正在从 Excel 中提取数据作为多维数组。当我尝试遍历数组以获取每个值时,获得的列表中很少有值包含撇号,而其他一些值则不包含。有什么办法可以纠正这个问题吗?
我试过使用 strip 函数,但效果不佳。 请帮忙。
我希望输出是
(2.56942078E+00, -8.59741137E-05, 4.19484589e-08, -1.00177799e-11, 1.22833691e-15, 29217.5791, 4.78433864)
但我最终得到了
('2.56942078E+00', '-8.59741137E-05', 4.19484589e-08, -1.00177799e-11, 1.22833691e-15, 29217.5791, 4.78433864)
撇号表明该项是字符串而不是浮点数。 '
只是字符串的表示。要将其用作浮点数,只需转换类型即可。
array = ('2.56942078E+00', '-8.59741137E-05', 4.19484589e-08, -1.00177799e-11, 1.22833691e-15, 29217.5791, 4.78433864)
for item in array:
print(type(item))
<class 'str'>
<class 'str'>
<class 'float'>
<class 'float'>
<class 'float'>
<class 'float'>
<class 'float'>
看看前两项是字符串而不是浮点数。 现在只需将项目转换为浮动,然后再对其进行其他操作:
for item in array:
item = float(item)
print(type(item))
<class 'float'>
<class 'float'>
<class 'float'>
<class 'float'>
<class 'float'>
<class 'float'>
<class 'float'>