Ignore lines with quotes or get DataWeave will to read invalid CSV

别说谁变了你拦得住时间么 提交于 2019-12-11 09:45:52

问题


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.



来源:https://stackoverflow.com/questions/34071644/ignore-lines-with-quotes-or-get-dataweave-will-to-read-invalid-csv

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!