MCPcopy Create free account
hub / github.com/apache/arrow / test_write_quoting_style

Function test_write_quoting_style

python/pyarrow/tests/test_csv.py:2093–2123  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

2091
2092
2093def test_write_quoting_style():
2094 t = pa.Table.from_arrays([[1, 2, None], ["a", None, "c"]], ["c1", "c2"])
2095 buf = io.BytesIO()
2096 for write_options, res in [
2097 (WriteOptions(quoting_style='none'), b'"c1","c2"\n1,a\n2,\n,c\n'),
2098 (WriteOptions(), b'"c1","c2"\n1,"a"\n2,\n,"c"\n'),
2099 (WriteOptions(quoting_style='all_valid'),
2100 b'"c1","c2"\n"1","a"\n"2",\n,"c"\n'),
2101 ]:
2102 with CSVWriter(buf, t.schema, write_options=write_options) as writer:
2103 writer.write_table(t)
2104 assert buf.getvalue() == res
2105 buf.seek(0)
2106
2107 # Test writing special characters with different quoting styles
2108 t = pa.Table.from_arrays([[",", "\""]], ["c1"])
2109 buf = io.BytesIO()
2110 for write_options, res in [
2111 (WriteOptions(quoting_style='needed'), b'"c1"\n","\n""""\n'),
2112 (WriteOptions(quoting_style='none'), pa.lib.ArrowInvalid),
2113 ]:
2114 with CSVWriter(buf, t.schema, write_options=write_options) as writer:
2115 try:
2116 writer.write_table(t)
2117 except Exception as e:
2118 # This will trigger when we try to write a comma (,)
2119 # without quotes, which is invalid
2120 assert isinstance(e, res)
2121 break
2122 assert buf.getvalue() == res
2123 buf.seek(0)
2124
2125
2126def test_write_quoting_header():

Callers

nothing calls this directly

Calls 3

WriteOptionsClass · 0.90
seekMethod · 0.80
write_tableMethod · 0.45

Tested by

no test coverage detected