summaryrefslogtreecommitdiffstats
path: root/Modules
diff options
context:
space:
mode:
authorSkip Montanaro <skip.montanaro@gmail.com>2023-04-12 22:32:30 (GMT)
committerGitHub <noreply@github.com>2023-04-12 22:32:30 (GMT)
commit330a942b6303c889d0f42f23d5ae2b42af92ecc4 (patch)
tree9cadad6696562afb944bd5b9b67f010148a08c99 /Modules
parent2b6e8777672da03f5d5cd12366e8378e47c550da (diff)
downloadcpython-330a942b6303c889d0f42f23d5ae2b42af92ecc4.zip
cpython-330a942b6303c889d0f42f23d5ae2b42af92ecc4.tar.gz
cpython-330a942b6303c889d0f42f23d5ae2b42af92ecc4.tar.bz2
gh-67230: add quoting rules to csv module (GH-29469)
Add two quoting styles for csv dialects. They will help to work with certain databases in particular. Automerge-Triggered-By: GH:merwok
Diffstat (limited to 'Modules')
-rw-r--r--Modules/_csv.c16
1 files changed, 15 insertions, 1 deletions
diff --git a/Modules/_csv.c b/Modules/_csv.c
index bd33708..2217cc2 100644
--- a/Modules/_csv.c
+++ b/Modules/_csv.c
@@ -82,7 +82,8 @@ typedef enum {
} ParserState;
typedef enum {
- QUOTE_MINIMAL, QUOTE_ALL, QUOTE_NONNUMERIC, QUOTE_NONE
+ QUOTE_MINIMAL, QUOTE_ALL, QUOTE_NONNUMERIC, QUOTE_NONE,
+ QUOTE_STRINGS, QUOTE_NOTNULL
} QuoteStyle;
typedef struct {
@@ -95,6 +96,8 @@ static const StyleDesc quote_styles[] = {
{ QUOTE_ALL, "QUOTE_ALL" },
{ QUOTE_NONNUMERIC, "QUOTE_NONNUMERIC" },
{ QUOTE_NONE, "QUOTE_NONE" },
+ { QUOTE_STRINGS, "QUOTE_STRINGS" },
+ { QUOTE_NOTNULL, "QUOTE_NOTNULL" },
{ 0 }
};
@@ -1264,6 +1267,12 @@ csv_writerow(WriterObj *self, PyObject *seq)
case QUOTE_ALL:
quoted = 1;
break;
+ case QUOTE_STRINGS:
+ quoted = PyUnicode_Check(field);
+ break;
+ case QUOTE_NOTNULL:
+ quoted = field != Py_None;
+ break;
default:
quoted = 0;
break;
@@ -1659,6 +1668,11 @@ PyDoc_STRVAR(csv_module_doc,
" csv.QUOTE_NONNUMERIC means that quotes are always placed around\n"
" fields which do not parse as integers or floating point\n"
" numbers.\n"
+" csv.QUOTE_STRINGS means that quotes are always placed around\n"
+" fields which are strings. Note that the Python value None\n"
+" is not a string.\n"
+" csv.QUOTE_NOTNULL means that quotes are only placed around fields\n"
+" that are not the Python value None.\n"
" csv.QUOTE_NONE means that quotes are never placed around fields.\n"
" * escapechar - specifies a one-character string used to escape\n"
" the delimiter when quoting is set to QUOTE_NONE.\n"