問題:
使用pprint輸出格式時,list對象過長,導致輸出換行,顯示不好看
print("Jobs: {}".format(pprint.pformat(raw_jobs)), file=out_file)
解決:
參考官方文檔,pformat參數compact控制長序列的每個項目是否在單獨的行上格式化。
class pprint.PrettyPrinter(indent=1, width=80, depth=None, stream=None, *, compact=False)
這句話的意思就是list對象是可迭代對象,如果總長度過長,對裏面的每個項目單獨輸出一行。至於到底多長是過長?同樣參考文檔,默認一行80個字符。
將輸出改爲
print("Jobs: {}".format(pprint.pformat(raw_jobs, compact=True)), file=out_file)