Smart_open: open() ignores newline for s3 csv

Created on 24 Aug 2020  路  2Comments  路  Source: RaRe-Technologies/smart_open

Problem description

I have a CSV on a S3 and I try to read it line for line with open(). The CSV has new lines (n) within the lines but a single line always ends with carriage return and new line (rn).
When I try to read the file, my lines always get split in the middle instead of the end. Setting the newline parameter seems to make no effect.

Steps/code to reproduce the problem

Examples I've tried:

open(uri='s3://my-bucket/my-file.csv', newline='\r\n')):
open(uri='s3://my-bucket/my-file.csv', mode='rb', newline='\r', encoding='UTF-8')):

my-file.csv:

column1,column2,column3\r\n
123,abc\n,456\r\n
234,bcd\n,567\r\n
345,cde\n,678\r\n

The lines get split like so:

column1,column2,column3
123,abc\n
456\r\n
...

instead of:

column1,column2,column3
123,abc\n456\r\n
...

Versions

Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 05:52:31) 
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
Darwin-18.7.0-x86_64-i386-64bit
Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 05:52:31) 
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)]
smart_open 2.1.0

Checklist

Before you create the issue, please make sure you have:

  • [x] Described the problem clearly
  • [x] Provided a minimal reproducible example, including any required data
  • [x] Provided the version numbers of the relevant software

Most helpful comment

How does Python's built-in open() behave on your CSV?

All 2 comments

How does Python's built-in open() behave on your CSV?

You're right @piskvorky. It behaves the same way.
I could fix my issue with the CSV module:

datareader = csv.DictReader(open('s3://my-bucket/my-file.csvv', encoding='utf-8'), delimiter=',')
        for order, row in enumerate(datareader):

I will close this issue. Thank you

EDIT: formatting

Was this page helpful?
0 / 5 - 0 ratings