Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MNT: decode and then slice the prefix off #21

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

tacaswell
Copy link

The length of a string as a string (e.g. in number of unicode codepoints) only matches the length in bytes as utf-8 for the first page (e.g. ascii).

In [3]: '\N{Snowman}'
Out[3]: '☃'

In [5]: '\N{Snowman}'.encode()
Out[5]: b'\xe2\x98\x83'

In [6]: prefix = '\N{Snowman}'

In [7]: a = prefix + '_bob'

In [8]: a
Out[8]: '☃_bob'

In [9]: a.encode()[len(prefix):].decode()
---------------------------------------------------------------------------
UnicodeDecodeError                        Traceback (most recent call last)
Cell In[9], line 1
----> 1 a.encode()[len(prefix):].decode()

UnicodeDecodeError: 'utf-8' codec can't decode byte 0x98 in position 0: invalid start byte

In [10]: len(prefix)
Out[10]: 1

In [11]: len(prefix.encode())
Out[11]: 3

The length of a string as a string (e.g. in number of unicode codepoints) only
matches the length in bytes as utf-8 for the first page (e.g. ascii).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant