我是Python新手,我正在嘗試編寫一個簡單的打印函數,但出現一個奇怪的錯誤。這是我的代碼:ValueError:類型爲'str'的對象的未知格式代碼'g'
#! /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])我非常感謝關於此問題的任何想法,謝謝;-)。
你實際上並沒有使用Python 3.2,是嗎?沒有Python 3的numpy的完成版本。另外,我無法在Windows上使用Python 2.7和Python 32位(它們是'int32'而不是'int64')來重現。這可能特定於某個版本的numpy或Python,或特定於64位Python。 – agf 2012-04-23 08:17:52
我正在使用Python 3.2.2,至少這是Python在命令行中輸入python3時在其版權聲明上方所說的內容! (我正在使用Linux) – makhlaghi 2012-04-23 08:21:06
添加'import sys;打印(sys.version)'到你的代碼,看看這個腳本實際運行的版本是什麼。 – agf 2012-04-23 08:35:51