[英]How to convert a unicode string with str type to a real unicode string in python2?
[英]Python returns string is both str and unicode type
我們有一個可以通過IronPython(2.7.5版)自定義的.NET應用程序
這是腳本代碼:
stringToPlay = # get it from our .NET app interface toward python here. The method returns .NET string
Log.Write("isinstance(stringToPlay, unicode): ", str(isinstance(stringToPlay, unicode)))
Log.Write("isinstance(stringToPlay, str): ", str(isinstance(stringToPlay, str)))
兩條日志行都會返回True?
stringToPlay值為“Ћирилица”。
當str和unicode應該是兩個獨立的類都繼承自basestring時,這怎么可能?
謝謝
IronPython在str
和unicode
之間沒有區別,如果你習慣了CPython 2語義,這可能會讓人很困惑。 實際上, basestring
和unicode
都是str
別名。
IronPython 2.7.4 (2.7.0.40) on .NET 4.0.30319.42000 (32-bit)
Type "help", "copyright", "credits" or "license" for more information.
>>> unicode
<type 'str'>
>>> basestring
<type 'str'>
關於字符串,IronPython(2.X)的行為更像Python 3,有一些令人討厭的事實,如果你想根據類型解碼/編碼你就無法區分unicode
和str
(因為它仍然是Python 2,也沒有bytes
)。
在IronPython中, str
類型和unicode
類型是同一個對象。 .NET字符串類型是unicode。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.