首页 > 解决方案 > 如果以 CSV 格式存储,如何将 \n 作为文本的一部分读取

问题描述

我已经设置delimiter="|"newline="\n"但是当我阅读文本时,文本中的 \n 并没有给我一个新的打印行。例如:

"Good Morning \nHow are you?"|"Its been a while since we met."

当我将对象作为列表的元素时csv.reader

with open ('file.csv', 'r', newline="\n") as f:
    r = csv.reader(f, delimiter='|')
    List = []
    for lines in r:
        List.append(lines)

List看起来像这样

["Good Morning \nHow are you?","Its been a while since we met."]

如果我运行代码

for i in list:
    print i

元素的预期输出将是

Good Morning
How are you?
Its been a while since we met.

但我最终得到的是

Good Morning \nHow are you?
Its been a while since we met.

而如果我直接分配List=["Good Morning \nHow are you?","Its been a while since we met."] ,我不会遇到这个问题。有什么解决方法吗?

我需要在文本中同时使用 CSV 和 \n。

标签: python-3.xlistcsvnewline

解决方案


推荐阅读