Python 3.4 support of print operations with redirectors >>
我在读一本课本上的一些例子。下面的源代码失败,具有以下回溯:
1 2 3 4 5 6 | 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' |
1 2 3 4 5 6 7 8 9 10 | 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() |
当我在python2.7下运行这段代码时,它会按预期工作。有人能给我指个方向吗?
在python 3中,
1 | print(key, file=dbfile) |
请看一下print是一个函数段落,来自官方文档中关于python 3中所做更改的内容。