Ignore lines with quotes or get DataWeave will to

2019-08-07 01:35发布

问题:

I'm trying to use Mule's DataWeave component to read a CSV file that isn't valid, or at least doesn't conform to RFC 4180. The issue is that there are some values that contain quotes, but the field isn't escaped. For example,

col1,col2,col3
one,two "two" two,three
one",two,three

Is there a way to straightforward way to slightly relax the rules in the CSV parser that DataWeave uses so that it will treat a value that does not start with a double-quote as a non-escaped value? Alternatively, can I (either using DataWeave or some other transformation) ignore all lines of text that have a quote in them? It's less than a fraction of one percent of the rows, and those rows by chance aren't relevant for this integration anyway, but I can't control the CSV generation.

edit: Here's an example:

CSV

Column A,Column B,Column C,Column D
A,Something Weird",C,D
A,B,Something Else" Weird,D,
A,",S,o,m,e,t,h,i,n,g, ,N,o,r,m,a,l,",C,D

DataWeave

%dw 1.0
%input payload application/csv
%output application/json
---
payload

Output

[
  {
    "Column A": "A",
    "Column B": ",C,D\r\nA,B,Something Else",
    "Column C": "D",
    "Column D": ""
  },
  {
    "Column A": "A",
    "Column B": ",S,o,m,e,t,h,i,n,g, ,N,o,r,m,a,l,",
    "Column C": "C",
    "Column D": "D"
  }
]

回答1:

Alternatively, can I (either using DataWeave or some other transformation) ignore all lines of text that have a quote in them?

Sure. Just remove all lines containing a double-quote from the input, before your DataWeave transformer.