python str spit失败,Python(pyspark)错误= ValueError:无法将字符串转换为float:“ 17”...

I am working with Python on Spark and reading my dataset from a .csv file whose first a few rows are:

17 0.2 7

17 0.2 7

39 1.3 7

19 1 7

19 0 7

When I read from the file line by line with the code below:

# Load and parse the data

def parsePoint(line):

values = [float(x) for x in line.replace(',', ' ').split(' ')]

return LabeledPoint(values[0], values[1:])

I get the this error:

Traceback (most recent call last):

File "", line 3, in parsePoint

ValueError: could not convert string to float: "17"

Any help is greatly appreciated.

解决方案

Following the comments below this answer, you should use:

[float(x.strip(' "')) for x in line.split(',')]

You do not need to replace ',' with ' ', you should simply split on , and then remove leading and trailing whitespaces and quotes (x.strip(' "')) before converting to float.

Also, have a look at the csv packages which may simplify your work.

Below is the answer to the original question (before comments).

You need to use .split() instead of .split(' '). You have multiple consecutive space characters in your line, so splitting on ' ' results in empty strings, e.g. your first line is split into:

['17', '', '0.2', '', '7']

The problem are those empty strings that you (obviously) cannot convert to float.

Using split() will solve the problem thanks to the behaviour of split when its sep argument is None (or not present):

If the optional second argument sep is absent or None, the words are separated by arbitrary strings of whitespace characters (space, tab, newline, return, formfeed).

See the doc of split, and a small example to understand the difference:

>>> sp5 = ' ' * 5

>>> sp5.split()

[]

>>> sp5.split(' ')

['', '', '', '', '', '']

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值