如何用python3制作unicode字符串

时间:2011-07-25 05:16:20

标签: python unicode python-3.x

我用过这个:

u = unicode(text, 'utf-8')

但是在Python 3中遇到错误(或者......我可能只是忘了包含一些内容):

NameError: global name 'unicode' is not defined

谢谢。

6 个答案:

答案 0 :(得分:111)

默认情况下,Python3中的文字字符串是unicode。

假设textbytes个对象,只需使用text.decode('utf-8')

Python2的

unicode相当于Python3中的str,所以你也可以写:

str(text, 'utf-8')

如果您愿意。

答案 1 :(得分:8)

What's new in Python 3.0说:

  

所有文字都是Unicode;但编码的Unicode表示为二进制   数据

如果您想确保输出utf-8,请参阅unicode in 3.0上此页面的示例:

b'\x80abc'.decode("utf-8", "strict")

答案 2 :(得分:8)

作为解决方法,我一直在使用它:

# Fix Python 2.x.
try:
    UNICODE_EXISTS = bool(type(unicode))
except NameError:
    unicode = lambda s: str(s)

答案 3 :(得分:0)

在我使用多年的Python 2程序中,有以下一行:

Sub AddToSUMMARY()
Dim ws As Worksheet

    ThisWorkbook.Sheets("Summary").Range("D13:E14").Clear
    For Each ws In ThisWorkbook.Worksheets
      If ws.Name <> "SUMMARY" Then
            ws.Range("D13:E14").Copy
            ThisWorkbook.Sheets("SUMMARY").Range("D13:D14").PasteSpecial _
            Paste:=xlAll, Operation:=xlAdd, SkipBlanks:=True, Transpose:=False
      End If
    Next
End Sub

这在Python 3中不起作用。

但是,该程序可用于:

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
            if(dy > 0) //check for scroll down
            {
                visibleItemCount = mLayoutManager.getChildCount();
                totalItemCount = mLayoutManager.getItemCount();
                pastVisiblesItems = mLayoutManager.findFirstVisibleItemPosition();

                if (loading) 
                {
                    if ( (visibleItemCount + pastVisiblesItems) >= totalItemCount) 
                    {
                        loading = false;
                        Log.v("...", "Last Item Wow !");
                        //Do pagination.. i.e. fetch new data
                    }
                }
            }
            if(dy < 0) //check for scroll up
            {
                visibleItemCount = mLayoutManager.getChildCount();
                pastVisiblesItems = mLayoutManager.findLastVisibleItemPosition();

                if (loading) 
                {
                    if ( (pastVisiblesItems - visibleItemCount) <= 0) 
                    {
                        loading = false;
                        Log.v("...", "First Item Wow !");
                        //Do pagination.. i.e. fetch new data
                    }
                }
            }
        }
    });

我不记得为什么首先在这里放置unicode,但是我认为这是因为该名称可以包含瑞典字母åäöÅÄÖ。但是即使它们没有“ unicode”也可以工作。

答案 4 :(得分:0)

python 3.x中最简单的方法

text = "hi , I'm text"
text.encode('utf-8')

答案 5 :(得分:0)

这是我解决问题的方式,例如将\ uFE0F,\ u000A等字符转换为16字节编码的表情符号。

example = 'raw vegan chocolate cocoa pie w chocolate &amp; vanilla cream\\uD83D\\uDE0D\\uD83D\\uDE0D\\u2764\\uFE0F Present Moment Caf\\u00E8 in St.Augustine\\u2764\\uFE0F\\u2764\\uFE0F '
import codecs
new_str = codecs.unicode_escape_decode(example)[0]
print(new_str)
>>> 'raw vegan chocolate cocoa pie w chocolate &amp; vanilla cream\ud83d\ude0d\ud83d\ude0d❤️ Present Moment Cafè in St.Augustine❤️❤️ '
new_new_str = new_str.encode('utf-16', 'surrogatepass').decode('utf-16')
print(new_new_str)
>>> 'raw vegan chocolate cocoa pie w chocolate &amp; vanilla cream??❤️ Present Moment Cafè in St.Augustine❤️❤️ '