Skip to content

Commit

Permalink
Use newline='' for csv.writer
Browse files Browse the repository at this point in the history
Fixes saulpw#1362

According to the docs for csv.writer:

> If csvfile is a file object, it should be opened with newline=''
> https://docs.python.org/3/library/csv.html#csv.writer

Additionally:

> If newline='' is not specified, newlines embedded inside quoted fields will
> not be interpreted correctly, and on platforms that use \r\n linendings 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.
> https://docs.python.org/3/library/csv.html#id3

This commit adds `newline=None` as a keyword argument for `open_text` in
`path.py`. It then uses this new keyword argument when opening a file
for writing in `loaders/csv.py`
  • Loading branch information
daviewales committed May 5, 2022
1 parent 3b18409 commit 2b31211
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 3 deletions.
2 changes: 1 addition & 1 deletion visidata/loaders/csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class CsvSheet(SequenceSheet):

def iterload(self):
'Convert from CSV, first handling header row specially.'
with self.source.open_text(encoding=self.options.encoding) as fp:
with self.source.open_text(encoding=self.options.encoding, newline='') as fp:
if options.safety_first:
rdr = csv.reader(removeNulls(fp), **options.getall('csv_'))
else:
Expand Down
4 changes: 2 additions & 2 deletions visidata/path.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ def __lt__(self, a):
def __truediv__(self, a):
return Path(self._path.__truediv__(a))

def open_text(self, mode='rt', encoding=None):
def open_text(self, mode='rt', encoding=None, newline=None):
'Open path in text mode, using options.encoding and options.encoding_errors. Return open file-pointer or file-pointer-like.'
# rfile makes a single-access fp reusable

Expand All @@ -183,7 +183,7 @@ def open_text(self, mode='rt', encoding=None):
vd.error('invalid mode "%s" for Path.open_text()' % mode)
return sys.stderr

return self.open(mode=mode, encoding=encoding or vd.options.encoding, errors=vd.options.encoding_errors)
return self.open(mode=mode, encoding=encoding or vd.options.encoding, errors=vd.options.encoding_errors, newline=newline)

@wraps(pathlib.Path.read_text)
def read_text(self, *args, **kwargs):
Expand Down

0 comments on commit 2b31211

Please sign in to comment.