You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Trying to print a data frame as plain, strict tsv (i.e., no quoting and no escaping, because I know none the fields will contain tabs), I wanted to use the "quoting" option, which is documented in pandas and is passed through to csv, as well as the "quotechar" option, not documented in pandas but also a csv option. But it doesn't work:
call in to_csv(), and doing corresponding changes to format.CSVFormatter()'s init() and save(), produces the expected output:
In [1]: importsys, csvIn [2]: frompandasimportDataFrameIn [3]: data= {'col1': ['contents of col1 row1', 'contents " of col1 row2'], 'col2': ['contents of col2 row1', 'contents " of col2 row2'] }
In [4]: df=DataFrame(data)
In [5]: df.to_csv(sys.stdout, sep='\t', quoting=csv.QUOTE_NONE, quotechar=None)
col1col20contentsofcol1row1contentsofcol2row11contents" of col1 row2 contents "ofcol2row2
i.e., unescaped, unquoted tsv.
More generally, there could be many reasons to want more control of the underlying csv writer, so a generic mechanism (as opposed to adding each param one by one) might be called for (e.g., allowign for a csv dialect object or at least a dictionary holding dialect attributes).
The text was updated successfully, but these errors were encountered:
yep...would be nice to add this parameter (and you are right, dialect would also be nice to pass, which if not None could control the values of other parms). Can you do a PR to add those (with tests!)
also I believe the doc string needs to be updated in to_csv and the docs in io.rst.
I'm not sure what a PR is, but I assume a pull request, given this is github? I confess I don't have a git repo of pandas, and did the above very quickly. I simply hacked the above mentioned modules directly so at least I could demonstrate the before and after behavior. I'll look into doing things properly, but it may take a while.
Trying to print a data frame as plain, strict tsv (i.e., no quoting and no escaping, because I know none the fields will contain tabs), I wanted to use the "quoting" option, which is documented in pandas and is passed through to csv, as well as the "quotechar" option, not documented in pandas but also a csv option. But it doesn't work:
Adding the parameter
quotechar=kwds.get("quotechar")
to the
formatter = fmt.CSVFormatter(...
call in to_csv(), and doing corresponding changes to format.CSVFormatter()'s init() and save(), produces the expected output:
i.e., unescaped, unquoted tsv.
More generally, there could be many reasons to want more control of the underlying csv writer, so a generic mechanism (as opposed to adding each param one by one) might be called for (e.g., allowign for a csv dialect object or at least a dictionary holding dialect attributes).
The text was updated successfully, but these errors were encountered: