2
我正在通过一些教科书中的例子。下面的源代码失败,出现以下回溯:Python 3.4支持与重定向器的打印操作>>
Traceback (most recent call last):
File "make_db_file.py", line 39, in <module>
storeDbase(db)
File "make_db_file.py", line 12, in storeDbase
print >> dbfile, key
TypeError: unsupported operand type(s) for >>: 'builtin_function_or_method' and '_io.TextIOWrapper'
def storeDbase(db, dbfilename=dbfilename):
"formatted dump of database to flat file"
import sys
dbfile = open(dbfilename, 'w')
for key in db:
print >> dbfile, key
for (name, value) in db[key].items():
print >> dbfile, name + RECSEP + repr(value)
print >> dbfile, ENDDB
dbfile.close()
当我运行Python 2.7下的代码它按预期工作。有人可以请我指出正确的方向。 print
函数中发生了哪些变化,以防止它在Python 3.4中起作用?
非常有帮助。这是在文档中吗? – dcrearer
@d_blk是的,我添加了一个链接到文档,它将Python 2中print()的更改与3关联。:) – Delgan
@dcrearer:您可以将此答案标记为已接受。 –