Python3 use csv file output

1 csv file output using dict:

import csv

outFile = "VehOutData/OrderIdSumTime.txt"

outFileCsv = open(outFile,"w",newline='')

fileheader = ['orderid','otime']
outDictWriter = csv.DictWriter(outFileCsv,fileheader)
outDictWriter.writeheader()

result =[ {'orderid':'abc','otime':44555},{'orderid':'abc','otime':44555}]
outDictWriter.writerows(result)

outFileCsv.close()
 

Note the use of spaces in order to reduce the line

open(outFile,"w",newline='')

Note the use mode is not w wb, newline = '' non-control line

The use of the same content under python2.7 version, wb is not the same.

And explained: https://blog.csdn.net/pfm685757/article/details/47806469

Published 36 original articles · won praise 0 · views 20000 +

Guess you like

Origin blog.csdn.net/weixin_38102912/article/details/81256510