SyntaxError: invalid string (possibly contains a unicode character)

Hello,

How do I solve this unicode error ?

I tried # -*- encoding: utf-8 -*- but it doesn’t help.

Thank you

Probably not the answer, but sometimes if your string is copied from somewhere, and pasted in to the editor, it can get some Unicode characters in it.

It happens to me with apostrophes a lot ( ’ ).

Here is an app to demonstrate a troublesome apostrophe:

https://anvil.works/build#clone:VSNVNJJB3RGVYKT5=C4QD6R5X3UAXIJ64AM4YS7W7

If your string is not big, can you take a guess at which characters are the troublesome ones and just rewrite them?

no, there are really these characters from my (Czech) language. But maybe I am doing something wrong.
When I for example specify some label in the designer it’s ok, but if I want to change them in the code I get this problem.

So for now I have all labels without diacritics, but later I will have to change it.

Oh okay. Perhaps someone else can help. It might be neat to see the string in question as well, this way others could play with it.

ok, here they are:

print “ěščřžýáí”

and screenshot of them :slight_smile: :
2019-06-16_23h54_43

The u prefix works for me:

self.label_1.text=u'ěščřžýáí'
2 Likes

thank you very much again, this works also perfectly for me !

1 Like

Ooh, I had a similar error from using a ‘%’ yesterday. I’ll try out this solution…

My understanding is that Client-side code is compiled from (roughly) Python 2.7, not Python 3, therefore the u prefix would be required for any non-ASCII string. Only for Python 3 code is the u unnecessary.

Your Server-side code may be Python 3.

4 Likes

Hello @p.colbert thank you for your message! I did wonder about this prefix - I have only been learning python 3, so haven’t seen it. but I do see the client side is v2.7 so will be aware there will be other differences from now on !

You’re welcome. The differences are not major. Only on occasion do I find them irritating.

For example, for speed of testing, I often write and test browser-side support routines on my desktop. But I then must re-test it in the browser. After all, I may have accidentally used some syntax specific to Python 3, or some library module or routine not in Skulpt (the Python->JavaScript transpiler). In that case, a thorough re-test is the only way to be sure. On the whole, though, it still saves time.

Ooh, I had a similar error from using a ‘%’ yesterday. I’ll try out this solution…

That’s funny, ‘%’ is part of the ASCII character set so you shouldn’t need a u prefix. What’s the exact string?

It’s not the %. It’s the apostrophes. Look closely, and you’ll see that they are different.

Aha, ‘smart quotes’. That often happens when you copy a string into a clever text editor (such as Slack) and out again - I would just replace them with normal apostrophes.

Of course, if you want smart quotes, you can indeed use u'‘these apostrophes are smart’'

Ok, so my mistake - I tried to use a £ in an axis label for a plotly graph object in my form and i got:

SyntaxError: invalid string (possibly contains a unicode character)

When I take out the offending £, or replace with u’£ million’, the form works.

Using the % sign does not cause an error and displays correctly.

In this case it wasn’t ‘smart quotes’, although i have come across this before and am now very wary before copying any text to use as code!

2 Likes