Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Schema - add quotechar option #11

Merged
merged 1 commit into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion magicparse/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,17 @@ class CsvSchema(Schema):
def __init__(self, options: Dict[str, Any]) -> None:
super().__init__(options)
self.delimiter = options.get("delimiter", ",")
self.quotechar = options.get("quotechar", '"')

def get_reader(self, stream: BytesIO) -> Iterable[List[str]]:
stream_reader = codecs.getreader(self.encoding)
stream_content = stream_reader(stream)

return csv.reader(
stream_content, delimiter=self.delimiter, quoting=csv.QUOTE_NONE
stream_content,
delimiter=self.delimiter,
quoting=csv.QUOTE_MINIMAL,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Juste pour être sur, pourquoi t'as besoin de modifier la valeur de "quoting" ?

quotechar=self.quotechar,
)

@staticmethod
Expand Down
40 changes: 40 additions & 0 deletions tests/test_schema.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from decimal import Decimal
from magicparse import Schema
from magicparse.schema import ColumnarSchema, CsvSchema
from magicparse.fields import ColumnarField, CsvField
Expand Down Expand Up @@ -215,6 +216,45 @@ def test_errors_do_not_halt_parsing(self):
]


class TestQuotingSetting(TestCase):
def test_no_quote(self):
schema = Schema.build(
{
"file_type": "csv",
"has_header": True,
"fields": [{"key": "column_1", "type": "decimal", "column-number": 1}],
}
)
rows, errors = schema.parse(b"column_1\n6.66")
assert rows == [{"column_1": Decimal("6.66")}]
assert not errors

def test_single_quote(self):
schema = Schema.build(
{
"file_type": "csv",
"quotechar": "'",
"has_header": True,
"fields": [{"key": "column_1", "type": "decimal", "column-number": 1}],
}
)
rows, errors = schema.parse(b"column_1\n'6.66'")
assert rows == [{"column_1": Decimal("6.66")}]
assert not errors

def test_double_quote(self):
schema = Schema.build(
{
"file_type": "csv",
"has_header": True,
"fields": [{"key": "column_1", "type": "decimal", "column-number": 1}],
}
)
rows, errors = schema.parse(b'column_1\n"6.66"')
assert rows == [{"column_1": Decimal("6.66")}]
assert not errors


class TestRegister(TestCase):
class PipedSchema(Schema):
@staticmethod
Expand Down
Loading