如何在视图中拆分包含非ascii字符的字符串?

时间:2022-06-12 15:30:52

In Python 2.7 and Django 1.8 postman views, I have this function in postman views:

在Python 2.7和Django 1.8 postman视图中,我在postman视图中有这个功能:

def mod1(message):
    print 'message is', message #bob>mary:سلام
    message = str(message) #without this I get 'Message' object has no attribute 'split'
    sndr = message.split('>')[0]
    print 'snder', sndr
    #...

Which give this error

哪个给出了这个错误

'ascii' codec can't decode byte 0xd8 in position 15: ordinal not in range(128)

Strangely, I can do the split in Python terminal.

奇怪的是,我可以在Python终端中进行拆分。

I have also added # -*- coding: utf-8 -*- at top of the views.

我还在视图顶部添加了# - * - coding:utf-8 - * - 。

Appreciate your hints to solve this.

感谢您解决此问题的提示。

1 个解决方案

#1


0  

Message contains some unicode text.

消息包含一些unicode文本。

If you are not careful to print it while encoding it properly, then you will get those errors.

如果您在正确编码时不小心打印它,那么您将得到这些错误。

They are because Python by default will try to encode using only the ASCII codec, which cannot handle the arabic characters. Usually you want to tell it to encode using UTF-8 or a similarly capable codec instead.

它们是因为默认情况下Python会尝试仅使用ASCII编解码器进行编码,而ASCII编解码器无法处理阿拉伯字符。通常,您希望告诉它使用UTF-8或类似功能的编解码器进行编码。

str(something).encode('UTF-8')

#1


0  

Message contains some unicode text.

消息包含一些unicode文本。

If you are not careful to print it while encoding it properly, then you will get those errors.

如果您在正确编码时不小心打印它,那么您将得到这些错误。

They are because Python by default will try to encode using only the ASCII codec, which cannot handle the arabic characters. Usually you want to tell it to encode using UTF-8 or a similarly capable codec instead.

它们是因为默认情况下Python会尝试仅使用ASCII编解码器进行编码,而ASCII编解码器无法处理阿拉伯字符。通常,您希望告诉它使用UTF-8或类似功能的编解码器进行编码。

str(something).encode('UTF-8')