【问题标题】:python Error : must be char not unicodepython错误:必须是char而不是unicode
【发布时间】:2017-11-21 04:47:27
【问题描述】:
def get_point_id(point_type, id):#id is str,like:"001",“1001.0”
    if not id:
        return None
    if True:
        str_id = str(int(id))
        if point_type == 'alarm':
            return str_id.rjust(3,'0')# error

当我运行它时,控制台显示:

文件“b2_insert_standard_template_signals.py”,第 118 行,在 get_point_id 中

return str_id.rjust(3,'0') TypeError: 必须是 char,而不是 unicode

str_id 是一个 str,不是 unicode,我不知道为什么。这个 py 文件已经声明了 # -- coding: utf-8 --

我的 python 版本是 2.7.3。请帮帮我。

【问题讨论】:

  • @rai_zhang 在 python 2.7.12 中运行您的代码。工作正常。没有错误。你能说明你是如何调用get_point_id 方法的吗?
  • 我只想格式化 id 。像这样: value=get_point_id(point_type, id), filnal ,vale会存储一个dict的值

标签: python-2.7


【解决方案1】:

好的,我知道发生了什么。因为我在.py文件的前面声明了“#--coding:utf-8--”。
所以 rjust 方法的第二个参数是 utf-8 编码。这是错误的。 rjust 方法的第二个参数必须是“str”。

很有趣

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-22
    • 1970-01-01
    • 2023-03-09
    • 2016-11-18
    • 2017-01-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多