【发布时间】:2014-08-03 10:01:00
【问题描述】:
当我运行我的代码时,我得到了这个错误:
UserId = "{}".format(source[1]) UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-3: ordinal not in range(128)
我的代码是:
def view_menu(type, source, parameters):
ADMINFILE = 'static/users.txt'
fp = open(ADMINFILE, 'r')
users = ast.literal_eval(fp.read())
if not parameters:
if not source[1] in users:
UserId = "{}".format(source[1])
users.append(UserId)
write_file(ADMINFILE,str(users))
fp.close()
reply(type, source, u"test")
else:
reply(type, source, u"test")
register_command_handler(view_menu, 'test', ['info','muc','all'], 0, '')
请问我该如何解决这个问题。
谢谢
【问题讨论】:
-
这里值得指出的是,这个问题正是 Python 3.x 存在的原因。您确定要学习在旧版本语言中混合 Unicode 和非 Unicode 字符串所需的所有笨拙的东西,只是为了在一两年内重新学习所有内容,而不是仅仅学习更容易和更新的东西现在呢?
标签: python python-2.7