Parse Text - peace098beat/windows_applicaciton GitHub Wiki
テキストをパースする. 愚直にやるならTextParserを使う.
204,800行2.33秒
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
//ストップウォッチを開始する
sw.Start();
// 圧力ファイルを開く
if (openFileDialog_pressure.ShowDialog() != DialogResult.OK) return;
// ファイル名描画
label1.Text = openFileDialog_pressure.FileName;
// Open CSV: http://stackoverflow.com/questions/18806757/parsing-csv-file-into-2d-array
string filePath = openFileDialog_pressure.FileName;
StreamReader sr = new StreamReader(filePath);
var lines = new List<string[]>();
while (!sr.EndOfStream)
{
string[] Line = sr.ReadLine().Split(null);
if (Line[0].StartsWith("BEGIN_DATA"))
{
textBox1.Text = string.Format("BEGIN_DATA");
while (true)
{
string[] DLine = sr.ReadLine().Split(null);
if (DLine[0].StartsWith("END_DATA")) break;
lines.Add(DLine);
}
}
}
var data = lines.ToArray();
//ストップウォッチを止める
sw.Stop();
textBox1.Text = string.Format("{0}", sw.Elapsed);
}
204,800行2.93秒
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
// 圧力ファイルを開く
if (openFileDialog_pressure.ShowDialog() == DialogResult.OK)
{
//ストップウォッチを開始する
sw.Start();
// ファイル名描画
label1.Text = openFileDialog_pressure.FileName;
//MICデータ
//var mic1 =
// パーサー
TextFieldParser parser = new TextFieldParser(openFileDialog_pressure.FileName);
parser.TextFieldType = FieldType.Delimited; // 区切られている
parser.SetDelimiters("\t"); // タブ区切り(TSVファイルの場合)
var lines = new List<string[]>();
//フラグ
string state = "";
// メインループ
while (parser.EndOfData == false)
{
// 一行読み込む
string[] column = parser.ReadFields();
/*************************************************************/
// 状態判定
/*************************************************************/
if (column[0].StartsWith("BEGIN_HEADER")) { state = "HEADER"; continue; }
else if (column[0].StartsWith("END_HEADER")) { state = ""; continue; }
else if (column[0].StartsWith("BEGIN_INFO")) { state = "INFO"; continue; }
else if (column[0].StartsWith("END_INFO")) { state = ""; continue; }
else if (column[0].StartsWith("BEGIN_DATA")) { state = "DATA"; continue; }
else if (column[0].StartsWith("END_DATA")) { state = ""; continue; }
/*************************************************************/
// -- 総列数を取得
/*************************************************************/
else if (column[0].StartsWith("Number_of_points"))
{
int number_of_points = int.Parse(column[1]);
// もし行数が0以下ならエラー
if (number_of_points <= 0) { MessageBox.Show("圧力ファイルの行数が0です"); }
// ループ終了
continue;
} // 総列数を取得
/*************************************************************/
/*************************************************************/
else if (state == "DATA")
{
// 一行読み込む
string[] _column = parser.ReadFields();
if (_column[0].StartsWith("END_DATA")) break;
lines.Add(_column);
}
//label2.Text = num_row.ToString();
//// デバッグ表示
//for (int i = 0; i < column.Length; i++)
//{
// textBox1.Text += ", "+ column[i] ;
//}
//textBox1.Text += "===\r\n";
// 随時処理
//Application.DoEvents();
//Thread.Sleep(0);
}
var data = lines.ToArray();
//ストップウォッチを止める
sw.Stop();
//結果を表示する
Debug.WriteLine(sw.Elapsed);
textBox1.Text = string.Format("{0}", sw.Elapsed);
}
} // openToolStripMenuItem_Click