我有一些代碼在Python 2.7中運行良好。Python 2.6的Str.format()給出錯誤,其中2.7不是
Python 2.7.3 (default, Jan 2 2013, 13:56:14)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from sys import stdout
>>> foo = 'Bar'
>>> numb = 10
>>> stdout.write('{} {}\n'.format(numb, foo))
10 Bar
>>>
但在2.6中,我得到一個ValueError異常。
Python 2.6.8 (unknown, Jan 26 2013, 14:35:25)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from sys import stdout
>>> foo = 'Bar'
>>> numb = 10
>>> stdout.write('{} {}\n'.format(numb, foo))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: zero length field name in format
>>>
當翻翻文件(2.6,2.7),我可以看到已在兩個版本之間沒有進行任何改變的提及。這裏發生了什麼?
謝謝。我只是忙着看着方法本身的文檔,因爲我一開始就錯過了文本。 – Mogget