I am trying to parse csv file that I don't control. The file contains quoted text like ...,text ""text"",...
and I want the parsed field to look like text "text"
.
Java Opencsv can parse this file just fine, but python library needs the whole field to be quoted (...,"text ""text""",...
) to properly unquote the text.
I've prepared a test suite
that illustrates my problem.
test.csv
col1,col2
idk,text ""text""
idk,"text ""text"""
idk,"text, text"
idk,text """"text""""
idk,"text """"text"""""
Expected output:
[
{'col1': 'idk', 'col2': 'text "text"'},
{'col1': 'idk', 'col2': 'text "text"'},
{'col1': 'idk', 'col2': 'text, text'},
{'col1': 'idk', 'col2': 'text ""text""'},
{'col1': 'idk', 'col2': 'text ""text""'},
]
Test program:
import csv
import unittest
from typing import Dict, List
def parse_input(path: str) -> List[Dict[str, str]]:
with open(path, 'r') as f:
reader = csv.DictReader(f)
return list(reader)
class TestStringMethods(unittest.TestCase):
def test_parsing(self) -> None:
parsed = parse_input('./test.csv')
self.assertEqual(
parsed,
[
{
'col1': 'idk',
'col2': 'text "text"',
},
{
'col1': 'idk',
'col2': 'text "text"',
},
{
'col1': 'idk',
'col2': 'text, text',
},
{
'col1': 'idk',
'col2': 'text ""text""',
},
{
'col1': 'idk',
'col2': 'text ""text""',
},
],
)
if __name__ == '__main__':
unittest.main()
I haven't found any parameters that would alow me to do what I want. I thought doublequote
parameter would work, but default value is already True
.
I have tried python built-in csv lib, pandas and clevercsv. They all treat the double quotes as simple characters not as one escaped double quote. Generally that is a good thing, but now I need to properly parse this file preferably without writing my own parser.
Do you have some suggestions on some python libraries which can parse this file?
idk,text ""text""
shall give{'col1': 'idk', 'col2': 'text ""text""'}
. Said differently why would you expect a csv library to read a file that is not csv encoded? If you use a custom format, you just need a custom parser."
anddoublequote
toFalse
when constructing the reader. Strictly speaking, there isn't really a strict standard for CSV, just a lot of common conventions.