在Python中将换行符写入csv

2024-02-09

我想通过将新的内容行(包括换行符)写入 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')
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在Python中将换行符写入csv 的相关文章

随机推荐