python 基础学习 u"string" 用法

u"string"
在string前面加上u,可以把string同意转换为Unicode类(统一码),便于规范输入输出

例子:
参考微信自动回复设置

# -*- coding: utf-8 -*
import itchat

itchat.auto_login()

#itchat.send("Hello,filehelper",toUserName = "filehelper")
@itchat.msg_register(itchat.content.TEXT)

def text_reply(msg):
    return '这里是'+ msg.text

itchat.auto_login()
itchat.run()

出错:

Login successfully as One Way
itchat has already logged in.
Start auto replying.
Traceback (most recent call last):
  File "/home/wei/anaconda3/envs/itchat/lib/python2.7/site-packages/itchat/components/register.py", line 60, in configured_reply
    r = replyFn(msg)
  File "chatit.py", line 10, in text_reply
    return '这里是'+ msg.text
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe8 in position 0: ordinal not in range(128)

更改:
在"这里是"前加上u

# -*- coding: utf-8 -*
import itchat

itchat.auto_login()

#itchat.send("Hello,filehelper",toUserName = "filehelper")
@itchat.msg_register(itchat.content.TEXT)

def text_reply(msg):
    return u'这里是'+ msg.text

itchat.auto_login()
itchat.run()
            

程序正常运行

Login successfully as One Way
itchat has already logged in.
Start auto replying.

微信可以自动回复发信人:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值