python - csv.writerows() puts newline after each row -
this example o'reilly cookbook (truncated dataset)
headers = ['symbol','price','date','time','change','volume'] rows = [{'symbol': 'aa', 'volume': 181800, 'change': -0.18,          'time': '9:36am', 'date': '6/11/2007', 'price': 39.48},         {'symbol': 'aig', 'volume': 195500, 'change': -0.15,          'time': '9:36am', 'date': '6/11/2007', 'price': 71.38} ]  open('stocks2.csv','w') f:     f_csv = csv.dictwriter(f, headers)     f_csv.writeheader()     f_csv.writerows(rows)   the output file has \n @ end of each line, , apparently 1 more @ end.  when bring excel, blank lines between each row.   same if open notepad++.
but, if more if command line, \n don't show up.
i saw q&a \n @ end of file - 1 \n @ end of each line.   (and don't see why more doesn't give \n.)
i plan bring file openoffice calc.
this problem occurs python on windows.
in python v3, need add newline='' in open call per:
python 3.3 csv.writer writes blank rows
on python v2, need open file binary "b" in open() call before passing csv
changing line
with open('stocks2.csv','w') f:   to:
with open('stocks2.csv','wb') f:   will fix problem
more info issue here:
Comments
Post a Comment