Sunday, 24 November 2019

remove non-breaking space from python code

I get an error while trying to import a module:

from sklearn.model_selection import train_test_split 

SyntaxError: invalid character in identifier

This is because there is a non breaking space at the end of the string:

x='from sklearn.model_selection import train_test_split '

x[-1:]

'\xa0'

I can replace that space and then copy-paste the code like this:

import unicodedata
new_str = unicodedata.normalize("NFKD", x)

print (new_str)

This new string have no issues:

from sklearn.model_selection import train_test_split

But I will like to know if ipython notebook has built-in function to correct such issues.



from remove non-breaking space from python code

No comments:

Post a Comment