3

I tried to run a test on Crab(an open source recommender system) based on python3. Then an error occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/Dennis/anaconda/lib/python3.5/site-packages/scikits/crab/datasets/base.py", line 201, in load_sample_movies
    data_songs[u_ix][i_ix] = float(rating)
ValueError: could not convert string to float: "b'3.0'"

I tried to use 'decode()' to convert the string, but it's not working:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/Dennis/anaconda/lib/python3.5/site-packages/scikits/crab/datasets/base.py", line 202, in load_sample_movies
    rating = rating.decode('utf-8')
AttributeError: 'numpy.str_' object has no attribute 'decode'  

Any help will be appreciated!

1
  • "b'3.0'" is unicode, the Py3 default string type. So it has an encode, but not decode. But that doesn't get ride of the extra layer of quoting. Commented Aug 5, 2016 at 16:36

1 Answer 1

2

The problem is that rating is a string within a string, so when you try casting a string like "b'3.0'" into a float, it gives a valueError because you still have the b in front which cannot be converted into float.

I imagine you need the byte encoding in front of the '3.0', so one way would be to evaluate rating to convert it from a string to bytes before typecasting it into a float (beware though, eval can have some safety issues).

>>> type(eval(rating))
<class 'bytes'>
>>> data_songs[u_ix][i_ix] = float(eval(rating))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.