用Python将换行符写入csv [英] Write newline to csv in Python
本文介绍了用Python将换行符写入csv的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想通过将一行新内容(包括换行符)写入一个csv文件来结束for循环的每个交互.我有这个:
I want to end each interation of a for loop with writing a new line of content (including newline) to a csv file. I have this:
# Set up an output csv file with column headers
with open('outfile.csv','w') as f:
f.write("title; post")
f.write("\n")
这似乎并未写入实际的\ n(换行符)文件.进一步:
This does not appear to write an actual \n (newline) the file. Further:
# Concatenate into a row to write to the output csv file
csv_line = topic_title + ";" + thread_post
with open('outfile.csv','w') as outfile:
outfile.write(csv_line + "\n")
这也不会将游标中的光标移动到下一行.每行新代码,以及循环的每次迭代,都会覆盖最新的一行.
This, also, does not move the cursor in the outfile to the next line. Each new line, with every iteration of the loop, just overwrites the most recent one.
我也尝试了outfile.write(os.linesep)
,但是没有用.
I also tried outfile.write(os.linesep)
but did not work.
推荐答案
将"w"更改为"a"
change 'w' to 'a'
with open('outfile.csv','a')
这篇关于用Python将换行符写入csv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文