首页 > 解决方案 > 使用 Python 写入 CSV 时如何将多个段落写入一个单元格

问题描述

我有 2 个变量正在使用 Python 写入 CSV。一个变量的数据,描述,是多个段落。如何防止新行写入新行?

from csv import writer
with open('test.csv', 'w', encoding='utf8', newline='') as file:
    thewriter = writer(file)
    header = ['Title', 'Description']
    thewriter.writerow(header)

    title = 'This is my title'
    description = 'This is my long description, with 3 paragraphs.\r\nThis is the 2nd paragrapgh\r\nAnd here the 3rd paragrapgh' 

    info = [title, description]
    print(info)
    thewriter.writerow(info)  

标签: pythoncsv

解决方案


上面的代码正在运行。感谢评论中的指导!


推荐阅读