Research on Chinese path processing in Python,

Source: Internet
Author: User

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?





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.