mirror of
https://github.com/kellyjonbrazil/jc.git
synced 2025-07-13 01:20:24 +02:00
fix doubleqoute in csv
This commit is contained in:
@ -130,9 +130,11 @@ def parse(data, raw=False, quiet=False):
|
||||
|
||||
if jc.utils.has_data(data):
|
||||
|
||||
dialect = None
|
||||
dialect = "excel" # default in csv module
|
||||
try:
|
||||
dialect = csv.Sniffer().sniff(data[:1024])
|
||||
if '""' in data:
|
||||
dialect.doublequote = True
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
3
tests/fixtures/generic/csv-doubleqouted.csv
vendored
Normal file
3
tests/fixtures/generic/csv-doubleqouted.csv
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
A,B
|
||||
1,"this is 1024 bytes long field this is 1024 bytes long field this is 1024 bytes long field this is 1024 bytes long field this is 1024 bytes long field this is 1024 bytes long field this is 1024 bytes long field this is 1024 bytes long field this is 1024 bytes long field this is 1024 bytes long field this is 1024 bytes long field this is 1024 bytes long field this is 1024 bytes long field this is 1024 bytes long field this is 1024 bytes long field this is 1024 bytes long field this is 1024 bytes long field this is 1024 bytes long field this is 1024 bytes long field this is 1024 bytes long field this is 1024 bytes long field this is 1024 bytes long field this is 1024 bytes long field this is 1024 bytes long field this is 1024 bytes long field this is 1024 bytes long field this is 1024 bytes long field this is 1024 bytes long field this is 1024 bytes long field this is 1024 bytes long field this is 1024 bytes long field this is 1024 bytes long field this is 1024 bytes long field this is 1024 bytes long field this"
|
||||
2,"this is a field with "" in it"
|
|
4
tests/fixtures/generic/csv-doubleqouted.json
vendored
Normal file
4
tests/fixtures/generic/csv-doubleqouted.json
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
[
|
||||
{"A": "1", "B": "this is 1024 bytes long field this is 1024 bytes long field this is 1024 bytes long field this is 1024 bytes long field this is 1024 bytes long field this is 1024 bytes long field this is 1024 bytes long field this is 1024 bytes long field this is 1024 bytes long field this is 1024 bytes long field this is 1024 bytes long field this is 1024 bytes long field this is 1024 bytes long field this is 1024 bytes long field this is 1024 bytes long field this is 1024 bytes long field this is 1024 bytes long field this is 1024 bytes long field this is 1024 bytes long field this is 1024 bytes long field this is 1024 bytes long field this is 1024 bytes long field this is 1024 bytes long field this is 1024 bytes long field this is 1024 bytes long field this is 1024 bytes long field this is 1024 bytes long field this is 1024 bytes long field this is 1024 bytes long field this is 1024 bytes long field this is 1024 bytes long field this is 1024 bytes long field this is 1024 bytes long field this is 1024 bytes long field this"},
|
||||
{"A": "2", "B": "this is a field with \" in it"}
|
||||
]
|
@ -37,6 +37,9 @@ class MyTests(unittest.TestCase):
|
||||
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/csv-insurance.csv'), 'r', encoding='utf-8') as f:
|
||||
self.generic_csv_insurance = f.read()
|
||||
|
||||
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/csv-doubleqouted.csv'), 'r', encoding='utf-8') as f:
|
||||
self.generic_csv_doubleqouted = f.read()
|
||||
|
||||
# output
|
||||
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/csv-biostats.json'), 'r', encoding='utf-8') as f:
|
||||
self.generic_csv_biostats_json = json.loads(f.read())
|
||||
@ -65,6 +68,9 @@ class MyTests(unittest.TestCase):
|
||||
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/csv-insurance.json'), 'r', encoding='utf-8') as f:
|
||||
self.generic_csv_insurance_json = json.loads(f.read())
|
||||
|
||||
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/csv-doubleqouted.json'), 'r', encoding='utf-8') as f:
|
||||
self.generic_csv_doubleqouted_json = json.loads(f.read())
|
||||
|
||||
def test_csv_nodata(self):
|
||||
"""
|
||||
Test with no data
|
||||
@ -125,6 +131,12 @@ class MyTests(unittest.TestCase):
|
||||
"""
|
||||
self.assertEqual(jc.parsers.csv.parse(self.generic_csv_insurance, quiet=True), self.generic_csv_insurance_json)
|
||||
|
||||
def test_doubleqouted(self):
|
||||
"""
|
||||
Test 'csv-doubleqouted.csv' file
|
||||
"""
|
||||
self.assertEqual(jc.parsers.csv.parse(self.generic_csv_doubleqouted, quiet=True), self.generic_csv_doubleqouted_json)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
Reference in New Issue
Block a user