【问题标题】:My Latin-1 coding tag is not working in python 3.4 but works fine in python 2.7我的 Latin-1 编码标签在 python 3.4 中不起作用,但在 python 2.7 中运行良好
【发布时间】:2014-06-03 22:33:34
【问题描述】:

我正在尝试使用 Bottle.py、Mongodb 和 Python 制作网页。

我的 python 2.7 代码顶部有这个标签:

# #-- coding: latin1 --

我使用编码来获取瓶子请求,例如:

CheckboxTr= bottle.request.forms.getlist("CheckboxTr")
tags = bottle.request.forms.get("tags")

我在每个网页的顶部也有meta charset="•ISO-8859-1",一切正常。但是,当我尝试迁移到 python 3.4 时,我没有收到任何错误消息,但我的编码似乎是 UTF-8 而不是 Latin-1(重音字符不会按应有的方式打印)。

我希望你能帮助我。我缺少迁移工作的一些东西,也许他们更新了编码名称或其他东西,但我一直在阅读 python 3.4 编码并且找不到我的错误。

这是代码:

@bottle.post('/newpost')
def post_newpost():
    title = bottle.request.forms.get("subject")
    post = bottle.request.forms.get("body")
    beneficios = bottle.request.forms.get("beneficios")
    CheckboxTramite= bottle.request.forms.getlist("CheckboxTramite")
    cookie = bottle.request.get_cookie("session")
    username = sessions.get_username(cookie)
    dependencia = sessions.get_dependencia(cookie)

return bottle.template("newpost_template1", dict(subject="", body = "", errors="", tags="", username=username, dependencia=dependencia, beneficios="", CheckboxTramite=[],))

【问题讨论】:

  • 我很确定那是wrong coding syntax。你错过了星星。它只在 Python 2 中“有效”,因为默认编码是 ASCII 而不是 UTF-8。
  • 我试过了:# -- coding: latin-1 --, # -- coding: iso-8859-15 --, #coding=latin-1
  • @HenryKeiter:不,没关系;它将匹配使用的正则表达式(参见PEP 263)。但是没有源代码,我们无能为力,只能在这里推测。
  • @MartijnPieters 啊,没错。看起来还是很奇怪。
  • @HenryKeiter:不;正则表达式是coding[:=]\s*([-\w.]+)"。所以coding 后跟冒号或等号,后跟可选的空格和实际的编解码器名称。它必须是评论,并且第一行或第二行,该行上的任何其他字符对于您想要识别评论的任何编辑器都是糖衣。

标签: python latin1


【解决方案1】:

我不知道为什么 Bottle 输出的编码会改变,因为它被记录为默认为 utf-8。但是the documentation 声明您可以通过设置Response.charset 属性来更改它。查看“更改默认编码”下的示例。

这与您的 Python 源文件的编码无关,因为该问题的一些 cmets 暗示了这一点。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-02
    • 2015-09-20
    • 2016-11-28
    • 1970-01-01
    • 2019-08-17
    • 1970-01-01
    相关资源
    最近更新 更多