问题描述
我正在读取 CSV 文件并将分隔符从,"更改为|".但是,我在我的数据(我无法控制)中注意到,在某些情况下,我有一些不想遵循此规则的数据,并且其中包含带逗号的引用数据.我想知道如何最好地不替换这些异常?
i'm reading a CSV file and changing the delimiter from a "," to a "|". However i've noticed in my data (which I have no control over) that in certain cases I have some data that does not want to follow this rule and it contains quoted data with a comma in it. I'm wondering how best to not replace these exceptions?
例如:
ABSON TE,Wick Lane,"Abson, Pucklechurch",Bristol,Avon,ENGLAND,BS169SD,37030,17563,BS0001A1,,,
应改为:
ABSON TE|Wick Lane|Abson, Pucklechurch"|Bristol|Avon|ENGLAND|BS169SD|37030|17563|BS0001A1||
读取和替换 CSV 文件的代码是这样的:
The code to read and replace the CSV file is this:
var contents = File.ReadAllText(filePath).Split(new string[] { "
", "
" }, StringSplitOptions.RemoveEmptyEntries).ToArray();
var formattedContents = contents.Select(line => line.Replace(',', '|'));
推荐答案
对于其他为此苦苦挣扎的人,我最终使用了内置的 .net csv 解析器.有关更多详细信息和示例,请参见此处:http://coding.abel.nu/2012/06/built-in-net-csv-parser/
For anyone else struggling with this, I ended up using the built in .net csv parser. See here for more details and example: 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();
这篇关于CSV 更改分隔符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!