ValueError:类型为"str"的对象的未知格式代码"g" [英] ValueError: Unknown format code 'g' for object of type 'str'
本文介绍了ValueError:类型为"str"的对象的未知格式代码"g"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是Python的新手,我试图编写一个简单的打印函数,但遇到一个奇怪的错误.这是我的代码:
I am new to Python and I am trying to write a simple print function but I am getting a strange error. This is my code:
#! /usr/bin/env python3.2
import numpy as np
a=np.arange(1,10,1)
print(a)
for i in a:
print(i)
print(type(i))
print("{0:<12g}".format(i))
输出为:
[1 2 3 4 5 6 7 8 9]
1
<class 'numpy.int64'>
Traceback (most recent call last):
File "./test.py", line 9, in <module>
print("{0:<12g}".format(i))
ValueError: Unknown format code 'g' for object of type 'str'
为什么打印将"numpy.int64"作为字符串?我必须补充一点,它非常适合普通列表:(例如[1,2,3,4]),感谢您对这个问题的任何想法,谢谢;-).
Why does print take the "numpy.int64" as a string? I have to add that it works perfectly for a normal list: (e.g. [1,2,3,4]) I would be most grateful to any ideas on this issue, thanks ;-).
推荐答案
这是已知错误,并且应在2.0版中进行修复.在此期间,您可以使用有效的旧语法%f
.
This is a known bug and should be fixed in version 2.0. In the interim, you can use the old syntax %f
that works.
这篇关于ValueError:类型为"str"的对象的未知格式代码"g"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文