我有一个data
,我想拆分并将其转换为float32
,但是它将实数显示为字符串
data = open('Path dataset')
for line in data:
train = np.array([np.float32(x) for x in line.split(",")[:]])
显示给我的错误是:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-83-53e8671c416d> in <module>
1 for line in data_coba:
----> 2 train = np.array([np.float32(x) for x in line.split(",")[:]])
3 #print(train_test_coba)
<ipython-input-83-53e8671c416d> in <listcomp>(.0)
1 for line in data_coba:
----> 2 train = np.array([np.float32(x) for x in line.split(",")[:]])
3 #print(train_test_coba)
ValueError: could not convert string to float: '50.89482266'
这有什么问题?
答案 0 :(得分:1)
似乎您的数据集包含的字符不只是逗号分隔的数字。因此,当尝试将这些非数字字符转换为float32时,可能会发生错误。我建议您再次检查数据集,然后尝试进一步拆分。
答案 1 :(得分:0)
您必须在打开功能中使用encoding ='utf-8'
data = open('Path dataset',encoding='utf-8')