a = '你们' 至 str 物

a = u'你们' 至 unicode 物


1.

>>> print 'u'  + '你们'

>>> u欢

输出乱码


2.

>>> print 'u'  + u'你'

>>> u你

正常


3.

>>> print 'u你'

>>> u浣

输出乱码


4.

>>> print 'u你' + 'u'

>>> u浣爑

输出乱码


5.

>>> print u'u你' + 'u'

>>> u你u

正常


6.

>>> print u'u你' + '你'

出现错误 UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position 0: ordinal not in range(128)

分析:'你'在内存中 为 0xe4。而python默认的编码方案是ascii,ascii无法识别0xe4


7.

>>> print u'u你' + u'你'

>>> u你你

正常


8.

>>> print 'u你' + u'你'

出现错误 UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position 1: ordinal not in range(128)


9.

>>> print 'u你'.decode('utf-8') + u'你'

>>> u你你

正常


10.

而在处理由系统採集的含有中文的路径时,使用string.decode('utf-8')就不一定行了,由于中文简体的windows系统默认编码为gb2312,繁体中文版会採用Big5码

实验步骤例如以下:

file_from = sys.argv[1] 为由系统採集的包括中文的路径

file_to = file_from[:file_from.rfind('\\')+1].decode('utf-8')  + u'你_' + file_from[file_from.rfind('\\')+1:].decode('utf-8')

print file_to

将出现错误:UnicodeDecodeError: 'utf8' codec can't decode byte 0xbb in position 24: invalid start byte

应该使用:decode('gb2312')

file_to = file_from[:file_from.rfind('\\')+1].decode('gb2312')  + u'你_' + file_from[file_from.rfind('\\')+1:].decode('gb2312') 

print file_to 正常


11.

而假设file_from是由你自己写入的包括中文的路径,如file_from = ‘c:\你.txt’

那么就应该用decode('utf-8')

能够參考上面的第7点和第9点