我需要阅读一个文本文件 (10mb) 并转换为 .csv。请参见下面的代码部分:
string DirPathForm = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);'
string[] lines = File.ReadAllLines(DirPathForm + @"\file.txt");
文本文件的某些部分具有模式。所以,使用如下:
string[] lines1 = lines.Select(x => x.Replace("abc[", "ab,")).ToArray();
Array.Clear(lines, 0, lines.Length);
lines = lines1.Select(x => x.Replace("] CDE ", ",")).ToArray();
有些部分没有模式可以直接使用替换。问题是如何删除这部分的字符、数字和空格。请看下面?
string[] lines = {
"a] 773 b",
"e] 1597 t",
"z] 0 c"
};
得到以下结果:
string[] result = {
"a,b",
"e,t",
"z,c"
};
obs:删除的项目需要用“,”替换。