因为python 3 print()函数允许end =“”定义,所以可以满足大多数问题。
就我而言,我想使用PrettyPrint并感到沮丧,因为该模块未进行类似的更新。 所以我做到了我想要的:
from pprint import PrettyPrinter
class CommaEndingPrettyPrinter(PrettyPrinter):
def pprint(self, object):
self._format(object, self._stream, 0, 0, {}, 0)
# this is where to tell it what you want instead of the default "\n"
self._stream.write(",\n")
def comma_ending_prettyprint(object, stream=None, indent=1, width=80, depth=None):
"""Pretty-print a Python object to a stream [default is sys.stdout] with a comma at the end."""
printer = CommaEndingPrettyPrinter(
stream=stream, indent=indent, width=width, depth=depth)
printer.pprint(object)
现在,当我这样做时:
comma_ending_prettyprint(row, stream=outfile)
我得到了我想要的(代替您想要的-您的里程可能会有所不同)