Research on Chinese path processing in Python,
A = 'You' is the str object
A = u'you' is a unicode object
1.
>>> Print 'U' + 'you'
>>> U Branch
Output garbled characters
2.
>>> Print 'U' + U' you'
>>> U you
Normal
3.
>>> Print 'U your'
>>> U Branch
Output garbled characters
4.
>>> Print 'U you '+ 'U'
>>> U zhuyun
Output garbled characters
5.
>>> Print u'u you '+ 'U'
>>> U you u
Normal
6.
>>> Print u'u you' + 'you'
Error UnicodeDecodeError: 'ascii 'codec can't decode byte 0xe4 in position 0: ordinal not in range (128)
Analysis: 'You' is 0xe4 in the memory, while the default python encoding scheme is ascii, ascii cannot recognize 0xe4
7.
>>> Print u'u you '+ u'you'
>>> U you
Normal
8.
>>> Print 'U you '+ U' you'
Error UnicodeDecodeError: 'ascii 'codec can't decode byte 0xe4 in position 1: ordinal not in range (128)
9.
>>> Print 'U your'. decode ('utf-8') + U' your'
>>> U you
Normal
10.
When processing the path containing Chinese characters collected by the system, use string. decode ('utf-8') is not necessarily enough, because the windows system of Simplified Chinese is encoded as gb2312 by default, and the Traditional Chinese version uses the Big5 code
The experiment process is as follows:
File_from = sys. argv [1] indicates the path containing Chinese characters collected by the system.
File_to = file_from [: file_from.rfind ('\') + 1]. decode ('utf-8') + U' _ '+ file_from [file_from.rfind (' \ ') + 1:]. decode ('utf-8 ')
Print file_to
Error: UnicodeDecodeError: 'utf8' codec can't decode byte 0xbb in position 24: invalid start byte
Use: decode ('gb2312 ')
File_to = file_from [: file_from.rfind ('\') + 1]. decode ('gb2312') + U' _ '+ file_from [file_from.rfind (' \ ') + 1:]. decode ('gb2312 ')
Print file_to is normal
11.
If file_from is a path written by you that contains Chinese characters, such as file_from = 'C: \ You .txt'
Then decode ('utf-8') should be used ')
Refer to the above 7th and 9th points
Please criticize and correct the deficiencies and errors !! Thank you !!
References:
Why you benefit from using UTF-8 Unicode everywhere in your web applicationsPython "'ascii 'codec can't decode byte" explained and how to solve it
Windows notepad ANSI, Unicode, UTF-8 what are the differences between these three encoding modes?