【问题标题】:Reading CSV file and storing values into an dictionary读取 CSV 文件并将值存储到字典中
【发布时间】:2016-06-03 17:24:55
【问题描述】:

我正在尝试读取test.csv 文件。像这样;

"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"

我可以使用StreamReader 读取test.csv 文件,并且可以使用Split() 函数分隔每一行。我想将每一列存储到一个单独的字典中,然后显示它。喜欢;

Console.WriteLine(GetText(test.try1.get1)); //OUTPUT: START (WITHOUT ("))

我认为,这段代码很糟糕。

-如何提高性能? -如何缩短此代码? -“代码的逻辑”好吗? -有人能帮我吗 ? (:

我的代码不工作;

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;
            }
        }
    }

【问题讨论】:

  • 我建议您考虑使用 CSV 解析库。

标签: c# arrays algorithm csv dictionary


【解决方案1】:

如果您可以保证文件的大小合理,我只需将整个文件读入内存并在其上运行正则表达式即可。然后遍历匹配项并将它们添加到字典中。

这个 SO 答案中给出的正则表达式应该适合你:

C#, regular expressions : how to parse comma-separated values, where some values might be quoted strings themselves containing commas

【讨论】:

  • 你能为此生成一个正则表达式吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-20
  • 1970-01-01
  • 1970-01-01
  • 2021-06-19
相关资源
最近更新 更多