分割由CSV文件组成的字符串

时间:2019-05-12 00:25:36

标签: c# arrays csv split

我有一个包含此类数据的CSV文件;

asset id,file_name,mime_type,created_by,email,country,description
51df6a98-614e-40ef-8885-95ae50940058,ElitProin.aam,application/x-authorware-map,sblack0,jmitchell0@huffingtonpost.com,United States,Maecenas ut massa quis augue luctus tincidunt. Nulla mollis molestie lorem. Quisque ut erat.
279899c1-bc63-495c-94c5-57f25c881ed2,MusVivamusVestibulum.xla,application/x-excel,iadams1,rhenry1@xrea.com,Canada,"Curabitur in libero ut massa volutpat convallis. Morbi odio odio, elementum eu, interdum eu, tincidunt in, leo. Maecenas pulvinar lobortis est."
3060b1db-9a0a-4046-b10a-b90a0c440070,NullaUltrices.sit,application/x-sit,jmatthews2,jadams2@ft.com,Japan,"Duis bibendum, felis sed interdum venenatis, turpis enim blandit mi, in porttitor pede justo eu massa. Donec dapibus. Duis at velit eu est congue elementum."
7ef8ddf4-0b60-43e6-9e17-21029da02c69,Eros.tsv,text/tab-separated-values,dadams3,jmoreno3@joomla.org,Serbia,"Curabitur in libero ut massa volutpat convallis. Morbi odio odio, elementum eu, interdum eu, tincidunt in, leo. Maecenas pulvinar lobortis est."
588610a3-b546-4c2e-822d-c822ba20a775,BlanditNam.aim,application/x-aim,dpierce4,aortiz4@devhub.com,United States,In congue. Etiam justo. Etiam pretium iaculis justo.
3de294a8-8f13-4f86-b447-92e8fd79277d,NecCondimentumNeque.jpe,image/jpeg,pyoung5,ckim5@ft.com,Romania,"Fusce posuere felis sed lacus. Morbi sem mauris, laoreet ut, rhoncus aliquet, pulvinar sed, nisl. Nunc rhoncus dui vel sem."
209bf3d7-e792-4f3f-98e8-240efcf81e31,DuisMattisEgestas.gif,image/gif,ldaniels6,mreid6@seattletimes.com,Belgium,"Curabitur in libero ut massa volutpat convallis. Morbi odio odio, elementum eu, interdum eu, tincidunt in, leo. Maecenas pulvinar lobortis est."
975eaf96-dd9f-4534-89a6-0ebabb730aa0,HacHabitassePlatea.arj,application/octet-stream,smorales7,jgarrett7@geocities.jp,Spain,In congue. Etiam justo. Etiam pretium iaculis justo.
5a8393df-d4aa-4734-8df4-10a611cd5cc7,NullaSuscipit.rt,text/vnd.rn-realtext,jschmidt8,rbowman8@prnewswire.com,United States,"Duis bibendum, felis sed interdum venenatis, turpis enim blandit mi, in porttitor pede justo eu massa. Donec dapibus. Duis at velit eu est congue elementum."
f41dd5e6-8833-4b22-8e33-edcb024c4c1f,Odio.mime,message/rfc822,cholmes9,rjones9@accuweather.com,Nigeria,"Fusce posuere felis sed lacus. Morbi sem mauris, laoreet ut, rhoncus aliquet, pulvinar sed, nisl. Nunc rhoncus dui vel sem."
74d04563-f253-4fcb-8caa-dfb2e1bba7ed,SuspendissePotentiCras.list,text/plain,tlawsona,sstevensa@ft.com,United States,"Proin eu mi. Nulla ac enim. In tempor, turpis nec euismod scelerisque, quam turpis adipiscing lorem, vitae mattis nibh ligula nec sem."

我已经将其加载到字符串变量中,因此我可以将其作为一个完整的字符串进行访问。但是我想将整个字符串分成多个部分,以其.csv逻辑对其进行访问。我使用“,”作为分隔符;

string[] values = s.Split(',');

这里的问题是,在我使用的.csv文件中,有一个“描述” 标头,并且在此标头下有包含逗号的文本。

"Curabitur in libero ut massa volutpat convallis. Morbi odio odio, elementum eu, interdum eu, tincidunt in, leo. Maecenas pulvinar lobortis est."

但是,这些逗号不应分隔整个“ csv字符串”,此文件的唯一定界符必须是分隔不同标头值的逗号。因为我要在一个完整的字符串中处理此csv文件,所以Split()方法也对这些位于文本值中的逗号应用了拆分操作。

如何处理此问题,以便获得模仿csv文件逻辑的拆分数组?任何帮助表示赞赏,谢谢..

1 个答案:

答案 0 :(得分:0)

您需要使用RegExp来解析csv: 这是一个例子:

Regex CSVParser = new Regex("^(?:([^",]+))?(?=,)|(?<=,)(?:[^",]*)?(?=[,$])|((?<=")[^"(\s*,)][^"]*(?="))|(?<=")(?=")|(?<=,)(?:[^,"])*(?=$)");
String[] Fields = CSVParser.Split(s);

通过搜索““用于读取csv文件的C#正则表达式”或类似的内容,您可以找到更多正则表达式示例。