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 c8cc7f4
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 @@ -47,7 +47,7 @@ def iterload(self):
@VisiData.api
def save_csv(vd, p, sheet):
'Save as single CSV file, handling column names as first line.'
with p.open_text(mode='w', encoding=sheet.options.encoding) as fp:
with p.open_text(mode='w', encoding=sheet.options.encoding, newline='') as fp:
cw = csv.writer(fp, **options.getall('csv_'))
colnames = [col.name for col in sheet.visibleCols]
if ''.join(colnames):
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 c8cc7f4

Please sign in to comment.