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

Function test_write_quoting_style

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

Source from the content-addressed store, hash-verified

2021
2022
2023def test_write_quoting_style():
2024 t = pa.Table.from_arrays([[1, 2, None], ["a", None, "c"]], ["c1", "c2"])
2025 buf = io.BytesIO()
2026 for write_options, res in [
2027 (WriteOptions(quoting_style='none'), b'"c1","c2"\n1,a\n2,\n,c\n'),
2028 (WriteOptions(), b'"c1","c2"\n1,"a"\n2,\n,"c"\n'),
2029 (WriteOptions(quoting_style='all_valid'),
2030 b'"c1","c2"\n"1","a"\n"2",\n,"c"\n'),
2031 ]:
2032 with CSVWriter(buf, t.schema, write_options=write_options) as writer:
2033 writer.write_table(t)
2034 assert buf.getvalue() == res
2035 buf.seek(0)
2036
2037 # Test writing special characters with different quoting styles
2038 t = pa.Table.from_arrays([[",", "\""]], ["c1"])
2039 buf = io.BytesIO()
2040 for write_options, res in [
2041 (WriteOptions(quoting_style='needed'), b'"c1"\n","\n""""\n'),
2042 (WriteOptions(quoting_style='none'), pa.lib.ArrowInvalid),
2043 ]:
2044 with CSVWriter(buf, t.schema, write_options=write_options) as writer:
2045 try:
2046 writer.write_table(t)
2047 except Exception as e:
2048 # This will trigger when we try to write a comma (,)
2049 # without quotes, which is invalid
2050 assert isinstance(e, res)
2051 break
2052 assert buf.getvalue() == res
2053 buf.seek(0)
2054
2055
2056def 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