Skip to content

fix: escape double quotes in the --output CSV file - #477

Open
LemonCondor wants to merge 1 commit into
dalibo:masterfrom
LemonCondor:fix/csv-output-quoting
Open

LemonCondor wants to merge 1 commit into
dalibo:masterfrom
LemonCondor:fix/csv-output-quoting

Conversation

@LemonCondor

Copy link
Copy Markdown

The CSV file written by --output can be read back with shifted columns when a value contains a double quote:

  • in the query, " was written as \", which is not how CSV escapes quotes;
  • in the database, application name and user fields, quotes were not escaped at all.

For example, with the query SELECT * FROM "Users";, Python's csv.reader(f, delimiter=";") returns 17 fields for a 16-column header, and the query comes back as SELECT * FROM \Users\".

This change doubles the quotes in every field (RFC 4180). The output is unchanged for values without double quotes. Anything that relied on the \" form would now see "" instead.

tests/test_csv_write.py fails on master for the four text fields and passes with the change. I also ran the doctests of pgactivity/utils.py, tests/test_reconnect.py, black, flake8, isort, codespell and mypy on the touched module. I did not run the tests that need a PostgreSQL server.

A double quote in a query was written as \" and quotes in the other text fields were not escaped at all, so CSV readers split the row on the wrong delimiters. Double the quotes in every field, as in RFC 4180.
@blogh

blogh commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

Hi thanks for contributing !

We have a MR on the same general area here, do you have an opinion on this?

@LemonCondor

Copy link
Copy Markdown
Author

I agree with you: pausing should freeze the display, not the recording. With --output, I would keep querying while paused or in interactive mode and append each new snapshot to the file, but keep the displayed rows separate, so that selection, cancel and terminate still apply to what is on screen. Without --output, nothing needs to change.

This PR only changes how values are quoted in utils.csv_write(), so it does not conflict with #476 and the two can be merged in either order.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants