I am trying to read a test.csv
file. Like this;
"test.try1.get1","START"
"test.try1.get2","Get 111"
"test.try1.get3","Get 222, 333"
"test.try1.get4","Get {test}, [{test2}, {test3]"
"test.try1.get5","Get \"{test}\", [{test2}, {test3]"
"test.try2.get1","Bla bla bla... bla bla bla...
Bla bla bla bla bla bla bla...
Bla bla bla bla bla bla bla... (:
BLA?"
"test.try2.get2","Bla bla bla... bla bla bla...
• 1 - Bla bla bla bla bla bla bla...
• 2 - Bla bla bla bla bla bla bla...
Bla bla bla \" bla bla bla bla... (: \"
BLA?"
"test.try3.get1","BONUSSS!!!"
"test.try3.get2","BONUSSS!!! \" "
"test.try4.get1","END"
I am able to read the test.csv
file using StreamReader
and able to separate each line by using the Split()
function. I want to store each column into a separate dictionary and then display it. Like ;
Console.WriteLine(GetText(test.try1.get1)); //OUTPUT: START (WITHOUT ("))
I think, this code is bad.
-How can I improve the performance ? -How do I shorten this code ? -Is "The logic of code" good ? -Can someone help me ? (:
MY NOT Working code ;
public static Dictionary<string, string> texts = new Dictionary<string, string>();
public static void LoadCSV() {
using (StreamReader reader = new StreamReader(@"test.csv")) {
string total = string.Empty;
bool multipleLine = false;
while (!reader.EndOfStream) {
string line = reader.ReadLine().ToString();
if (!multipleLine && line.StartsWith("\"") && !line.EndsWith("\"") && line != string.Empty) {
multipleLine = true;
}
if(multipleLine && line == string.Empty) {
total += "\n\n";
continue;
}
if (!multipleLine && line == string.Empty) {
continue;
}
if (multipleLine && !line.StartsWith("\"") && !line.EndsWith("\"") && line != string.Empty) {
total += line;
continue;
}
else if (multipleLine && !line.StartsWith("\"") && line.EndsWith("\"") && line != string.Empty) {
total += line;
multipleLine = false;
}
if (!multipleLine && line.StartsWith("\"") && line.EndsWith("\"") && line != string.Empty) {
total = line;
}
string[] splitted = total.Split(',');
if (!texts.ContainsKey(splitted[0])) {
//Index out of range
texts.Add(splitted[0].Replace("\"", string.Empty), splitted[1]);
Console.WriteLine(string.Format("ADDED : {0} , {1}", splitted[0], splitted[1]));
}
total = string.Empty;
multipleLine = false;
}
}
}