StringIO的“ rU”模式(在未加引号的字段中看到换行符)

时间:2019-04-10 15:39:01

标签: python csv gzip stringio

我正在尝试逐行解析gzip文件:

with gzip.open(obj.get()['Body'])as f:

    for line in f:
        line=StringIO(line.decode("utf-8"))
        line=csv.reader(line,delimiter=',')

        for line1 in line:

         #some logic

但是对于某些文件,我有错误:

new-line character seen in unquoted field - do you need to open the file in universal-newline mode?

当我尝试以换行模式打开时:

csv.reader(open(line, 'rU'), delimiter=',')

我有:

expected str, bytes or os.PathLike object, not _io.StringIO

我希望所有包含'\ r'的字段都在该字段中,作为字符串值的一部分。 该如何解决?

2 个答案:

答案 0 :(得分:0)

类似这样的事情,避免使用csv.readerStringIO模块:

with gzip.open(obj.get()['Body'])as f:
    for line in f:
        line = line.strip()
        line = line.decode("utf-8").split(',')

        for line1 in line:
            #some logic

答案 1 :(得分:0)

根据https://docs.python.org/3.7/library/io.html?highlight=io#io.StringIO,如果您将第二个参数传递为None,它将识别所有换行符