相关疑难解决方法(0)

NameError:未定义全局名称'unicode' - 在Python 3中

我正在尝试使用名为bidi的Python包.在这个包中的模块(algorithm.py)中,有一些行给出了错误,尽管它是包的一部分.

以下是这些行:

# utf-8 ? we need unicode
if isinstance(unicode_or_str, unicode):
    text = unicode_or_str
    decoded = False
else:
    text = unicode_or_str.decode(encoding)
    decoded = True
Run Code Online (Sandbox Code Playgroud)

这是错误信息:

Traceback (most recent call last):
  File "<pyshell#25>", line 1, in <module>
    bidi_text = get_display(reshaped_text)
  File "C:\Python33\lib\site-packages\python_bidi-0.3.4-py3.3.egg\bidi\algorithm.py",   line 602, in get_display
    if isinstance(unicode_or_str, unicode):
NameError: global name 'unicode' is not defined
Run Code Online (Sandbox Code Playgroud)

我应该如何重写这部分代码,以便它在Python3中工作?如果有人使用Python 3的bidi包,请告诉我他们是否发现了类似的问题.我感谢您的帮助.

python unicode bidi nameerror python-3.x

120
推荐指数
3
解决办法
16万
查看次数

将索引号转换为 int (Python)

编辑: .mean() 函数工作得恰到好处:

renda['age'].mean()
Run Code Online (Sandbox Code Playgroud)

我仍然不确定为什么会出现下面和答案中提到的错误,但这有助于作为捷径。


我有一个 DataFrame renda,其中有一列age,我需要得到年龄的平均值。我写

t_age = renda['age'].sum()
renda.index = renda.index.map(int) #intended solution
last_id = renda.iloc(-1)
print(t_age/last_id)
Run Code Online (Sandbox Code Playgroud)

试图从这里调整答案。但它仍然不起作用,并引发了此错误:

TypeError: unsupported operand type(s) for /: 'int' and '_iLocIndexer'
Run Code Online (Sandbox Code Playgroud)

我能做些什么来解决这个问题?谢谢!


@斯科特波士顿:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/opt/conda/lib/python3.6/site-packages/pandas/core/ops.py in na_op(x, y)
    657             result = expressions.evaluate(op, str_rep, x, y,
--> 658                                           raise_on_error=True, **eval_kwargs)
    659         except TypeError:

/opt/conda/lib/python3.6/site-packages/pandas/core/computation/expressions.py in evaluate(op, op_str, a, b, raise_on_error, use_numexpr, **eval_kwargs)
    210         return _evaluate(op, op_str, a, b, raise_on_error=raise_on_error,
--> 211 …
Run Code Online (Sandbox Code Playgroud)

python indexing int pandas

5
推荐指数
2
解决办法
2万
查看次数

标签 统计

python ×2

bidi ×1

indexing ×1

int ×1

nameerror ×1

pandas ×1

python-3.x ×1

unicode ×1