-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCSVReader.cs
41 lines (34 loc) · 1.33 KB
/
CSVReader.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;
public class CSVReader
{
public static string SPLIT_RE = @",(?=(?:[^""]*""[^""]*"")*(?![^""]*""))";
public static string LINE_SPLIT_RE = @"\r\n|\n\r|\n|\r";
public static char[] TRIM_CHARS = { '\"' };
public static List<Dictionary<string, string>> Read()
{
var file = "C:/Users/user/Desktop/Wordle/Words.csv";
var list = new List<Dictionary<string, string>>();
string data = System.IO.File.ReadAllText(file);
var lines = Regex.Split(data, LINE_SPLIT_RE);
if (lines.Length <= 1) return list;
var header = Regex.Split(lines[0], SPLIT_RE);
for (var i = 1; i < lines.Length; i++)
{
var values = Regex.Split(lines[i], SPLIT_RE);
if (values.Length == 0 || values[0] == "") continue;
var entry = new Dictionary<string, string>();
for (var j = 0; j < header.Length && j < values.Length; j++)
{
string value = values[j];
value = value.TrimStart(TRIM_CHARS).TrimEnd(TRIM_CHARS).Replace("\\", "");
string finalvalue = value;
entry[header[j]] = finalvalue;
}
list.Add(entry);
}
return list;
}
}