unicode 和 python 字串的差異

unicode 例如: u'\u53f0\u7063\u65b0\u86cb\u7db2'

和 python 字串其實不同, 例如: '字串'

python 呼叫 print 可接受的 input 參數是 python 字串

如果用 unicode 字串輸入進去會有 error
這時候要用 utf-8 encode 才有辦法 print  -->  title.encode('utf-8')
其中 title 為 u'\u53f0\u7063\u65b0\u86cb\u7db2'
print 才不會出錯


又, 有時候我們會看到諸如這樣的字串: r'C:\Users\zouber\Desktop\Tagtoo Git\Tagtoo\trunk\dashboard'

參考資料在此: http://stackoverflow.com/questions/2081640/what-exactly-do-u-and-rstring-flags-in-python-and-what-are-raw-string-litte

據參考資料說, 這是代表字串裡頭的 \  將會被當成純文字來解讀, 而不會有平常的神奇魔力(跳脫字元的腳色)

留言

熱門文章