我想通过将新的一行内容(包括换行符)写入 csv 文件来结束 for 循环的每次交互。我有这个:
# Set up an output csv file with column headers
with open('outfile.csv','w') as f:
f.write("title; post")
f.write("\n")
这似乎并没有写入实际的\n(换行符)文件。进一步:
# 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")
这也不会将输出文件中的光标移动到下一行。循环的每次迭代中的每一行都会覆盖最近的一行。
我也尝试了 outfile.write(os.linesep)
但没有成功。
最佳答案
将'w'更改为'a'
with open('outfile.csv','a')
关于python - 在 Python 中将换行符写入 csv,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43733205/