我想创建一个.csv文件,其中包含我从其他文件中找到并复制的一些数据。我用行变量格式化csv。问题是5和6值被跳到下一行。我做错了什么?
Cols名称很好,但是后来脚本出了问题,不确定在哪里以及为什么。
col1 col2 col3 col4 col5 col6 col7
filename a1 a2 a3 a4 a5 a6
但相反我回忆道:
col1 col2 col3 col4 col5 col6 col7
filename a1 a2 a3 a4
a5 a6
我的代码如下所示:
string getDir = Directory.GetCurrentDirectory();
DirectoryInfo d = new DirectoryInfo(@getDir + "\\src");
FileInfo[] Files = d.GetFiles("*.txt");
string str = "";
foreach(FileInfo file in Files )
{
str = str + "," + file.Name;
}
string lines="col1," + "col2," + "col3," + "col4 ," + "col5," + "col6," + "\n";
int ncheck = 1;
int countFiles = d.GetFiles().Length;
int vcheck = 0;
while (ncheck <= /*countFiles*/1)
{
var getfile = str.Split(',')[ncheck];
lines = lines + getfile;
while ( vcheck <= 4)
{
string startSTR = "a1,a2,a3,a4,a5";
var starts = startSTR.Split(',')[vcheck];
string endSTR = "b1,b2,b3,b4,b5";
var ends = endSTR.Split(',')[vcheck];
string St = System.IO.File.ReadAllText(d + "\\" + getfile);
int pFrom = St.IndexOf(starts) + starts.Length;
if (St.IndexOf(starts) == -1 || St.IndexOf(ends) == -1) {pFrom=0;};
int pTo = St.LastIndexOf(ends);
if (St.IndexOf(starts) == -1 || St.IndexOf(ends) == -1) {pTo=pFrom+0;};
String result = St.Substring(pFrom, pTo - pFrom);
if (St.IndexOf(starts) == -1 || St.IndexOf(ends) == -1) {result="Brak danych";};
/*Console.WriteLine(starts);
Console.WriteLine(result);
Console.WriteLine(ends);*/
Console.WriteLine(lines);
lines = lines + "," + result;
Console.WriteLine(lines);
vcheck++;
}
lines = lines + "\n";
ncheck++;
vcheck=0;
}
System.IO.StreamWriter filetxt = new System.IO.StreamWriter(@getDir + "\\test.csv");
filetxt.WriteLine(lines);
filetxt.Close();