■ CSV 파서를 사용하는 방법을 보여준다.
▶ CSVParser.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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
using System; using System.Collections.Generic; using System.IO; using System.Text; namespace TestProject { /// <summary> /// CSV 파서 /// </summary> public static class CSVParser { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 파싱하기 - Parse(reader, delimiter, qualifier) /// <summary> /// 파싱하기 /// </summary> /// <param name="reader">텍스트 리더</param> /// <param name="delimiter">구분자</param> /// <param name="qualifier">품질 구분자</param> /// <returns>문자열 리스트 열거 가능형</returns> public static IEnumerable<IList<string>> Parse(TextReader reader, char delimiter, char qualifier) { bool inQuote = false; List<string> recordList = new List<string>(); StringBuilder stringBuilder = new StringBuilder(); while(reader.Peek() != -1) { char characterRead = (char)reader.Read(); if(characterRead == '\n' || (characterRead == '\r' && (char) reader.Peek() == '\n')) { if(characterRead == '\r') { reader.Read(); } if(inQuote) { if(characterRead == '\r') { stringBuilder.Append('\r'); } stringBuilder.Append('\n'); } else { if(recordList.Count > 0 || stringBuilder.Length > 0) { recordList.Add(stringBuilder.ToString()); stringBuilder.Clear(); } if(recordList.Count > 0) { yield return recordList; } recordList = new List<string>(recordList.Count); } } else if(stringBuilder.Length == 0 && !inQuote) { if(characterRead == qualifier) { inQuote = true; } else if(characterRead == delimiter) { recordList.Add(stringBuilder.ToString()); stringBuilder.Clear(); } else if(char.IsWhiteSpace(characterRead)) { } else { stringBuilder.Append(characterRead); } } else if(characterRead == delimiter) { if (inQuote) { stringBuilder.Append(delimiter); } else { recordList.Add(stringBuilder.ToString()); stringBuilder.Clear(); } } else if(characterRead == qualifier) { if(inQuote) { if((char) reader.Peek() == qualifier) { reader.Read(); stringBuilder.Append(qualifier); } else { inQuote = false; } } else { stringBuilder.Append(characterRead); } } else { stringBuilder.Append(characterRead); } } if(recordList.Count > 0 || stringBuilder.Length > 0) { recordList.Add(stringBuilder.ToString()); } if(recordList.Count > 0) { yield return recordList; } } #endregion #region 머리말/꼬리말 파싱하기 - ParseHeadAndTail(reader, delimiter, qualifier) /// <summary> /// 머리말/꼬리말 파싱하기 /// </summary> /// <param name="reader">텍스트 리더</param> /// <param name="delimiter">구분자</param> /// <param name="qualifier">품질 구분자</param> /// <returns>(문자열 리스트, 문자열 리스트 열거 가능형) 튜플</returns> public static Tuple<IList<string>, IEnumerable<IList<string>>> ParseHeadAndTail(TextReader reader, char delimiter, char qualifier) { return ParseHeadAndTail(Parse(reader, delimiter, qualifier)); } #endregion //////////////////////////////////////////////////////////////////////////////// Private #region 꼬리말 열거하기 - EnumerateTail<T>(sourceEnumerable) /// <summary> /// 꼬리말 열거하기 /// </summary> /// <typeparam name="T">타입</typeparam> /// <param name="sourceEnumerable">소스 열거 가능형</param> /// <returns>열거 가능형</returns> private static IEnumerable<T> EnumerateTail<T>(IEnumerator<T> sourceEnumerable) { while(sourceEnumerable.MoveNext()) { yield return sourceEnumerable.Current; } } #endregion #region 머리말/꼬리말 파싱하기 - ParseHeadAndTail<T>(sourceEnumerable) /// <summary> /// 머리말/꼬리말 파싱하기 /// </summary> /// <typeparam name="T">타입</typeparam> /// <param name="sourceEnumerable">소스 열거 가능형</param> /// <returns>(타입, 타입 열거 가능형) 튜플</returns> private static Tuple<T, IEnumerable<T>> ParseHeadAndTail<T>(this IEnumerable<T> sourceEnumerable) { if(sourceEnumerable == null) { throw new ArgumentNullException("sourceEnumerable"); } IEnumerator<T> enumerator = sourceEnumerable.GetEnumerator(); enumerator.MoveNext(); return Tuple.Create(enumerator.Current, EnumerateTail(enumerator)); } #endregion } } |
▶ Program.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 42 43 44 45 46 47 48 49 50 |
using System; using System.Collections.Generic; using System.IO; namespace TestProject { /// <summary> /// 프로그램 /// </summary> class Program { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 프로그램 시작하기 - Main() /// <summary> /// 프로그램 시작하기 /// </summary> private static void Main() { string source = @" '100','200','100','300','400' '200','300','400','200','100' '100','300','200','300','400' "; using(StringReader reader = new StringReader(source)) { IEnumerable<IList<string>> lineEnumerable = CSVParser.Parse(reader, ',', '\''); foreach(IList<string> itemEnumerable in lineEnumerable) { foreach(string item in itemEnumerable) { Console.Write(item); Console.Write(" "); } Console.WriteLine(); } } } #endregion } } |