Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从csv读取字符串不会打印\n_Python_Python 3.x_Csv_Newline - Fatal编程技术网

Python 从csv读取字符串不会打印\n

Python 从csv读取字符串不会打印\n,python,python-3.x,csv,newline,Python,Python 3.x,Csv,Newline,我正在从csv文件中读取字符串。这些字符串包含“\n”字符以表示新行。 如何使用正确的新行打印这些字符串?使用str()无效 CSV文件只有一行: Dest,Test\n Test2\n Test3 下面的代码打印Test\n Test2\n Test3 for ifile in files: with open(orderPath + '\\' + ifile) as csv_file: csv_reader = csv.reader(csv_file, delimi

我正在从csv文件中读取字符串。这些字符串包含“\n”字符以表示新行。 如何使用正确的新行打印这些字符串?使用str()无效

CSV文件只有一行:

Dest,Test\n Test2\n Test3
下面的代码打印
Test\n Test2\n Test3

for ifile in files:
    with open(orderPath + '\\' + ifile) as csv_file:
        csv_reader = csv.reader(csv_file, delimiter=',')

        for row in csv_reader:

            dest = row[0]
            message = row[1]

            message = str(message)
            print(message)
import ast
import csv

filename = 'one_line.csv'

with open(filename, newline='') as csv_file:
    for row in csv.reader(csv_file, delimiter=','):
        dest = row[0]
        message = ast.literal_eval('"""{}"""'.format(row[1]))
        print(dest)
        print(message)

如果我理解正确,您希望用换行符替换输入中的文字字符“\”和“n”序列:

而不是:

message = str(message)
你想要:

message = message.replace('\\n', '\n')

目前还不清楚您希望如何解释csv文件中的数据,但这里有一个猜测。它使用函数将输入数据中的转义字符转换为换行字符

这涉及到用三个双引号(
“”“
)字符将第二列括起来,以允许将其解释为Python字符串文字。如果所括文本本身包含引号,则使用三个引号。例如:
Dest,Test\n“Test2”\n Test3

for ifile in files:
    with open(orderPath + '\\' + ifile) as csv_file:
        csv_reader = csv.reader(csv_file, delimiter=',')

        for row in csv_reader:

            dest = row[0]
            message = row[1]

            message = str(message)
            print(message)
import ast
import csv

filename = 'one_line.csv'

with open(filename, newline='') as csv_file:
    for row in csv.reader(csv_file, delimiter=','):
        dest = row[0]
        message = ast.literal_eval('"""{}"""'.format(row[1]))
        print(dest)
        print(message)
输出:

Dest
试验
测试2
测试3

能否尝试在open()函数中传递newline=''参数?这是否回答了您的问题?您希望得到什么?csv文件中的一行应该如何解释?谢谢,这正是我需要的!