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

Function test_write_quoting_style

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

Source from the content-addressed store, hash-verified

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