Skip to content
Advertisement

Parse multiple line CSV using PySpark , Python or Shell

Input (2 columns) :

JavaScript

Note: Harry and Prof. does not have starting quotes

Output (2 columns)

JavaScript

What I tried (PySpark) ?

JavaScript

Issue The above code worked fine where multiline had both start and end double quotes (For eg: row starting with Ronald)

But it didnt work with rows where we only have end quotes but no start quotes (like Harry and Prof)

Even if we add start quotes with Harry and Prof that will solve the issue

Any idea using Pyspark , Python or Shell , etc are welcome !!

Advertisement

Answer

Based solely on the small sample provided:

  • remove all double quotes
  • there are two comma-delimited fields; 1st field is a string, 2nd field is a number
  • the 1st field may contain commas and may be broken across multiple lines
  • replace the comma delimiter with a pipe (|)
  • OP’s expected output is inconsistent with regards to spacing before the newly inserted pipe (|); sometimes a space is removed, sometimes a space is inserted; for now we won’t worry about spacing

One awk idea:

JavaScript

This generates:

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement