Issue
I have a pipe-delimited data set where some of the values also have pipes in them. These elements are enclosed by \\ on either side to denote that pipes in between them should not be used as delimiters. The raw data looks like:
Col1|Col2|Col3
1|some text|more text
2|some text|more text
3|\\text with a | in it\\|more text
4|\\a|b|c\\|more text
I want to read these into a pandas dataframe so that it looks like:
| Col1 | Col2 | Col3 |
|---|---|---|
| 1 | some text | more text |
| 2 | some text | more text |
| 3 | text with a | in it | more text |
| 4 | a|b|c | more text |
Attempt 1
If I just use
pd.read_csv(path, sep='|')
I get the error
---------------------------------------------------------------------------
ParserError Traceback (most recent call last)
...
pandas/_libs/parsers.pyx in pandas._libs.parsers.raise_parser_error()
ParserError: Error tokenizing data. C error: Expected 3 fields in line 3, saw 4
becuase the engine interprets row 3 as having 4 columns.
Attempt 2
I thought this would be solved using the quotechar parameter (reference to docs)
pd.read_csv(path, sep='|', quotechar='\\')
but this will leave the values as NaN rather than correctly parsed
| Col1 | Col2 | Col3 |
|---|---|---|
| 1 | some text | more text |
| 2 | some text | more text |
| 3 | NaN | more text |
| 4 | NaN | more text |
Attempt 3
I tried using the comment parameter (though I don't think this is its intended use and got the same result as Attempt 2.
pd.read_csv(path, sep='|', comment='\\')