2012-06-18 44 views
1

我遇到一个csv文件中的字符问题,通过黑钻与一个?在中间。适应csv阅读器读取unicode字符

我已经编写了解析csv的代码,但我不明白为什么字符串没有正确读取unicode字符。这可能与我的实现有关:

StreamReader readFile = new StreamReader(path) 

try { 
    while ((line = readFile.ReadLine()) != null) { 
    string[] row = { "", "", "" }; 
    int currentItem = 0; 
    bool inQuotes = false; 
    if (skippedFirst && currentItem != 3) { 
     for (int i = 0; i < line.Length; i++) { 
     if (!inQuotes) { 
      if (line[i] == '\"') 
      inQuotes = true; 
      else { 
      if (line[i] == ',') 
       currentItem++; 
      else 
       row[currentItem] += line[i]; 
      } 
     } else { 
      if (line[i] == '\"') 
      inQuotes = false; 
      else 
      row[currentItem] += line[i]; 
     } 
     } 
     parsedFile.Add(row); 
    } 
    skippedFirst = true; 
    } 
+1

如果'readFile'是'StreamReader',你可以使用与编码部分的构造:'Encoding.UTF8 '。 –

+0

显示'readFile'的创建。 – leppie

+0

@ Trustme-I'maDoctor把它作为答案!这有助于出色 – ediblecode

回答

4

打开文件时指定编码。

using (var sr = new StreamReader(@"c:\Temp\csvfile.csv", Encoding.UTF8)) { 
} 

你也可能想看看Filehelpers为CSV解析:

http://www.filehelpers.com/quick_start.html

+0

没有工作。 ReadLine()仍然给出 字符 – ediblecode

+1

你确定csv是utf8编码的吗?也许这是不同的。拉丁语1或类似的东西。编码问题是你*知道它是什么,因为它不可能正确地检测到它。 – mfussenegger

+0

谢谢,我认为这是像Encoding.GetEncoding(1212) – ediblecode