CSV更改定界符 [英] CSV change delimiter
问题描述
我正在读取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,布里斯托尔,埃文,英格兰,BS16
9SD,37030,17563,BS0001A1,
ABSON TE,Wick Lane,"Abson, Pucklechurch",Bristol,Avon,ENGLAND,BS16 9SD,37030,17563,BS0001A1,,
应更改为:
ABSON TE | Wick Lane | Abson,Pucklechurch |布里斯托尔|雅芳| ENGLAND | BS16
9SD | 37030 | 17563 | BS0001A1 ||
ABSON TE|Wick Lane|"Abson, Pucklechurch"|Bristol|Avon|ENGLAND|BS16 9SD|37030|17563|BS0001A1||
读取和替换CSV文件的代码是这个:
The code to read and replace the CSV file is this:
var contents = File.ReadAllText(filePath).Split(new string[] { "\n", "\r\n" }, 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更改定界符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!