python - python3 TypeError:'bytes'对象不可调用
问题描述
我的代码:
for i in range(data.num_nodes):
if embed[i]:
# print embed[i]
tmp = np.sum(embed[i], axis=0) / len(embed[i])
file.write(' '.join(map(str.encode("utf-8"), tmp)) + '\n')
else:
file.write('\n')
但是,当我运行代码时,我得到:
file.write(' '.join(map(str.encode("utf-8"), tmp)) + '\n')
`TypeError: 'bytes' object is not callable`
当我将代码更改为:
for i in range(data.num_nodes):
if embed[i]:
# print embed[i]
tmp = np.sum(embed[i], axis=0) / len(embed[i])
file.write(' '.join(map(str, tmp)) + '\n')
else:
file.write('\n')
我收到此错误:
TypeError: a bytes-like object is required, not 'str'
解决方案
做:
file.write(' '.join(map(str.encode, tmp)) + '\n')
代替:
file.write(' '.join(map(str.encode("utf-8"), tmp)) + '\n')
因为str.encode
需要一个字符串的参数,这是因为默认它已经utf-8
编码
推荐阅读
- python - 原始错误是:没有名为“numpy.core._multiarray_umath”的模块
- spring - 如何在spring批处理的处理器中为每个文档调用子查询
- python - 使用 ModelSerializer 序列化一对多关系
- python - AWS Athena - 获取当前日期的 QueryExecutionIds
- r - R. 使用 R. 预测时间序列。无法绘制自动图层
- java - 用 Java 设计 JSON
- java - 快速字节数组反序列化
- mysql - 触发 SQL 表停车率
- javascript - 如何使文本对齐适用于强标签?
- django - 如何在不使用 for 循环的情况下以弹出模式显示数据?