今天幫同事研究一個莫名其妙的UnicodeDecodeError時發現了Python字串格式化中的一個小陷阱,在此記錄一下。原本的程式碼過於複雜,有太多與問題無關的東西,所以我在ipython裡簡單試驗復現了問題,過程如下:
In [4]: a = '你好世界' In [5]: print 'Say this: %s' % a Say this: 你好世界 In [6]: print 'Say this: %s and say that: %s' % (a, 'hello world') Say this: 你好世界 and say that: hello world In [7]: print 'Say this: %s and say that: %s' % (a, u'hello world') --------------------------------------------------------------------------- UnicodeDecodeError Traceback (most recent call last) /home/jerry/ in () UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position 10: ordinal not in range(128) In [8]: a Out[8]: '\xe4\xbd\xa0\xe5\xa5\xbd\xe4\xb8\x96\xe7\x95\x8c'
看到In [7]之後的那個很怪異的UnicodeDecodeError沒?它和上一句的唯一區別是’hello world‘變成了一個unicode物件而非str物件。但問題是,’hello world’只是單純的英文字串,不包含任何ASCII以外的字符,怎麼會無法decode呢?再仔細看看異常附帶的message,裡面提到了0xe4,這個顯然不是'hello world'裡面的,所以只能懷疑那句中文了,In [8]把它的字節序列打印了出來,果然就是它,第一個就是0xe4。
看來在字串格式化的時候Python試圖將a decode成unicode對象,並且decode時用的還是預設的ASCII編碼而非實際的UTF-8編碼。那這又是怎麼回事呢? ?下面繼續我們的試驗:
In [9]: 'Say this: %s' % 'hello' Out[9]: 'Say this: hello' In [10]: 'Say this: %s' % u'hello' Out[10]: u'Say this: hello'
仔細看,In [9]中的'hello'是普通的字串,結果也是字串(str對象),而In [10]中的'hello'變成了unicode對象,格式化的結果也變成unicode了(注意結果開頭的那個u)。
所以真相是這樣的:Python在格式化字串的時候有一些隱藏著的小動作:如果%s對應的參數裡有unicode,那麼最終的結果也是unicode。在這種情況下模版字串以及所有的%s參數中的str都會被decode成unicode,然而這個decode是隱式的,用戶無法指定其使用的charset,Python只能用預設的ASCII。如果剛好裡面有非ASCII編碼的字串,就完蛋了…
看看Python文檔怎麼說的:
If format is a Unicode object, or if any of the objects being converted using the %s conversion are Unicode objects, the result will also be a Unicode object.
如果程式碼裡混合著str和unicode,這種問題很容易出現。在同事的程式碼裡,中文字串是用戶輸入的,經過了正確的編碼處理,是以UTF-8編碼的str對象;但那個惹事的unicode對象,雖然其內容都是ASCII碼,但其來源是sqlite3資料庫查詢的結果,而sqlite的API回傳的字串都是unicode對象,所以導致了這麼怪異的結果。
Python 2的str和unicode真的挺坑爹,已經被它們害過好幾次了。 Python 3在這方面很有提升,期待它的全面普及!

numpyArraysareAreBetterFornumericalialoperations andmulti-demensionaldata,而learthearrayModuleSutableforbasic,內存效率段

numpyArraySareAreBetterForHeAvyNumericalComputing,而lelethearRayModulesiutable-usemoblemory-connerage-inderabledsswithSimpleDatateTypes.1)NumpyArsofferVerverVerverVerverVersAtility andPerformanceForlargedForlargedAtatasetSetsAtsAndAtasEndCompleXoper.2)

ctypesallowscreatingingangandmanipulatingc-stylarraysinpython.1)usectypestoInterfacewithClibrariesForperfermance.2)createc-stylec-stylec-stylarraysfornumericalcomputations.3)passarraystocfunctions foreforfunctionsforeffortions.however.however,However,HoweverofiousofmemoryManageManiverage,Pressiveo,Pressivero

Inpython,一個“列表” isaversatile,mutableSequencethatCanholdMixedDatateTypes,而“陣列” isamorememory-sepersequeSequeSequeSequeSequeRingequiringElements.1)列表

pythonlistsandArraysareBothable.1)列表Sareflexibleandsupportereceneousdatabutarelessmory-Memory-Empefficity.2)ArraysareMoremoremoremoreMemoremorememorememorememoremorememogeneSdatabutlesserversEversementime,defteringcorcttypecrecttypececeDepeceDyusagetoagetoavoavoiDerrors。

Python和C 各有優勢,選擇應基於項目需求。 1)Python適合快速開發和數據處理,因其簡潔語法和動態類型。 2)C 適用於高性能和系統編程,因其靜態類型和手動內存管理。

選擇Python還是C 取決於項目需求:1)如果需要快速開發、數據處理和原型設計,選擇Python;2)如果需要高性能、低延遲和接近硬件的控制,選擇C 。

通過每天投入2小時的Python學習,可以有效提升編程技能。 1.學習新知識:閱讀文檔或觀看教程。 2.實踐:編寫代碼和完成練習。 3.複習:鞏固所學內容。 4.項目實踐:應用所學於實際項目中。這樣的結構化學習計劃能幫助你係統掌握Python並實現職業目標。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

Atom編輯器mac版下載
最受歡迎的的開源編輯器

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

禪工作室 13.0.1
強大的PHP整合開發環境

WebStorm Mac版
好用的JavaScript開發工具