在使用Python读取csv时指定换行符('\ n') [英] Specify Newline character ('\n') in reading csv using Python

查看:814
本文介绍了在使用Python读取csv时指定换行符('\ n')的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Python 3读取每行由换行符('\ n')决定的csv文件.这是我的代码:

I want to read a csv file with each line dictated by a newline character ('\n') using Python 3. This is my code:

import csv
with open(input_data.csv, newline ='\n') as f:
        csvread = csv.reader(f)
        batch_data = [line for line in csvread]

上面的代码给出了错误:

This above code gave error:

batch_data = [line for line in csvread].
_csv.Error: new-line character seen in unquoted field - do you need to open the file in universal-newline mode?

阅读这些帖子:在未引用字段错误,还尝试了我可以考虑的以下替代方法:

Reading these posts: CSV new-line character seen in unquoted field error, also tried these alternatives that I could think about:

with open(input_data.csv, 'rU', newline ='\n') as f:
        csvread = csv.reader(f)
        batch_data = [line for line in csvread]


with open(input_data.csv, 'rU', newline ="\n") as f:
        csvread = csv.reader(f)
        batch_data = [line for line in csvread]

没有运气得到正确的答案.有什么建议吗?

No luck of geting this correct yet. Any suggestions?

我还在阅读有关换行符的文档:如果换行符未指定='',嵌入引号字段中的换行符将无法正确解释,并且在使用\ r \ n行写的平台上将添加一个额外的\ r.指定newline =''应该永远是安全的,因为csv模块会执行自己的(通用)换行符处理.

I am also reading the documentation about newline: if newline='' is not specified, newlines embedded inside quoted fields will not be interpreted correctly, and on platforms that use \r\n line on write an extra \r will be added. It should always be safe to specify newline='', since the csv module does its own (universal) newline handling.

所以我对这种换行方法的理解是:

So my understanding of this newline method is:

1)这是必须的,

2)它是否表示输入文件将被空格分隔成几行?

2) does it indicate the input file would be split into lines by empty space character?

推荐答案

  1. newline=''在所有csv情况下都是正确的,并且在许多情况下未指定它是错误.文档出于您遇到的原因而推荐它.

  1. newline='' is correct in all csv cases, and failing to specify it is an error in many cases. The docs recommend it for the very reason you're encountering.

newline=''并不意味着使用空白"进行拆分;它是记录在open函数上的:

newline='' doesn't mean "empty space" is used for splitting; it's specifically documented on the open function:

如果[newline]为'',则启用通用换行模式,但行尾未翻译返回给呼叫者.

If [newline] is '', universal newlines mode is enabled, but line endings are returned to the caller untranslated.

因此,对于newline='',所有原始\r\n字符均保持不变.通常,在通用换行模式下,任何类似于序列的换行(\r\n\r\n)都会在输入中转换为\n.但是您不希望将其用于CSV输入,因为CSV方言通常对构成换行符的内容非常挑剔(Excel方言仅要求\r\n即可.)

So with newline='' all original \r and \n characters are returned unchanged. Normally, in universal newlines mode, any newline like sequence (\r, \n, or \r\n) is converted to \n in the input. But you don't want this for CSV input, because CSV dialects are often quite picky about what constitutes a newline (Excel dialect requires \r\n only).

您的代码应为:

import csv
with open('input_data.csv', newline='') as f:
    csvread = csv.reader(f)
    batch_data = list(csvread)

如果这不起作用,则需要查看CSV方言并确保正确初始化csv.reader.

If that doesn't work, you need to look at your CSV dialect and make sure you're initializing csv.reader correctly.

这篇关于在使用Python读取csv时指定换行符('\ n')的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆