CSV更改分隔符

时间:2014-08-12 16:12:21

标签: c# .net asp.net-mvc-4 .net-4.5

我正在读取CSV文件并更改","的分隔符。到" |"。但是我已经注意到我的数据(我无法控制),在某些情况下,我有一些数据不想遵循此规则,并且它包含带逗号的引用数据。我想知道如何最好不要取代这些例外?

例如:

  

ABSON TE,Wick Lane," Abson,Pucklechurch",Bristol,Avon,ENGLAND,BS16   9SD,37030,17563,BS0001A1 ,,

应改为:

  

ABSON TE | Wick Lane |" Abson,Pucklechurch" |布里斯托尔|雅芳|英格兰| BS16   9SD | 37030 | 17563 | BS0001A1 ||

读取和替换CSV文件的代码如下:

var contents = File.ReadAllText(filePath).Split(new string[] { "\n", "\r\n" }, StringSplitOptions.RemoveEmptyEntries).ToArray();
var formattedContents = contents.Select(line => line.Replace(',', '|'));

1 个答案:

答案 0 :(得分:2)

对于其他正在努力解决这个问题的人,我最终使用了内置的.net csv解析器。有关详细信息和示例,请参见此处:http://coding.abel.nu/2012/06/built-in-net-csv-parser/

我的具体代码:

 // Create new parser object and setup parameters
var parser = new TextFieldParser(new StringReader(File.ReadAllText(filePath)))
{
    HasFieldsEnclosedInQuotes = true,
    Delimiters = new string[] { "," },
    TrimWhiteSpace = true
};

var csvSplitList = new List<string>();

// Reads all fields on the current line of the CSV file and returns as a string array
// Joins each field together with new delimiter "|"
while (!parser.EndOfData)
{
    csvSplitList.Add(String.Join("|", parser.ReadFields()));
}

// Newline characters added to each line and flattens List<string> into single string
var formattedCsvToSave = String.Join(Environment.NewLine, csvSplitList.Select(x => x));

// Write single string to file
File.WriteAllText(filePathFormatted, formattedCsvToSave);
parser.Close();