or is it necessary to intercept all the bytes of the Chinese character?
I get in the program of the string may be < br / > print(< strong > 'ab string < / strong >. The encode() [r]. 0-4 decode()) < br / > may be < br / > print(< STR ong>' string ' .encode()[0:4].decode())
Since UTF-8 encoding must be used,
when the bold part is a variable,
I get variable values are not necessarily, I intercept the first 4 bits and decode the error
How can I avoid it?
For example, when you cut out half a character, you discard it.
The method I think is try and catch, but UTF8 takes 3 characters, sometimes one more, sometimes two more, how to deal with it?
0 Answer
or is it necessary to intercept all the bytes of the Chinese character?
For example, do you want to intercept the first four bytes of 'ab string 'to mean 'ab character'?
two step transcoding results, you need to obtain what data?
Post an example?
According to your thinking, can you consider one or both more cases?
I want to intercept file names no more than 9 bytes Chinese characters are UTF8 encoding, taking up 3, so I need encode to intercept again,
So when you get a file name of "one, two, three" you can cut the first nine digits.
But the file name is "1, 1, 2, 3" and I'll get an error if I cut the first 9 digits.
May I ask how to handle such an exception?
< div class = " aw-list-img " >
First slice in transcoding can not do it
' string '[0:4].encode().decode
or say how how something
I want to intercept file names no more than 9 bytes Chinese characters are UTF8 encoding, taking up 3, so I need encode to intercept again,
So when you get a file name of "one, two, three" you can cut the first nine digits.
But the file name is "1, 1, 2, 3" and I'll get an error if I cut the first 9 digits.
May I ask how to handle such an exception?
What do you need if the file name is 1, 1, 2, 3? If it is 1, 1, 2, the first three digits are captured by decode.
no error, I need to get "1, 1, 2". Remove the last two extra characters.
Shall I try catch two ifs... I think there might be a better way, so I came to ask
这家伙很懒,什么都没留下...