user3541631
user3541631

Reputation: 4008

Trying to split string, but I have issues related to encoding

I want to split a string , in two base on the last space in the string.

Example:

    full_text = "0.808 um"
    value, unit = full_text.rsplit(" ")

This should have worked bu I get the error:

 ValueError: not enough values to unpack (expected 2, got 1)

So I printed, on what happens on split:

['0.808\xa0um']

In my example the string is static, but in reality I receive them from a database, and I don't know when a space is space or not.

I want to maintain the encoding for characters (not space) received, but also want to split.

Upvotes: 0

Views: 37

Answers (1)

deceze
deceze

Reputation: 522175

You would simply need to expect more and different kinds of whitespace to split by. In your case you're dealing with a no-break space. The regular expression \s would match it and a few other kinds of whitespace:

>>> import re
>>> re.split(r'\s', '0.808\xa0um')
['0.808', 'um']

Upvotes: 2

Related Questions