2017-05-24 91 views
-1

我使用此代码省略了引号并用逗号分隔。 我有这样的数据csv文件的内容。 ex:
“1111”,“05-24-2017”,“08:30”,“0”,“TRAVEL”
“2222”,“05-25-2017”,“08:20”, “0”,“旅行”用双引号括起来,并用csv文件中的逗号分隔

我用这个代码:

public bool ReadEntrie(int id, ref string name) 
    { 
     int count = 0; 
     CreateConfigFile(); 
     try 
     { 
      fs = new FileStream(data_path, FileMode.Open); 
      sr = new StreamReader(fs); 
      bool cond = true; 
      string temp = ""; 
      while (cond == true) 
      { 
       if ((temp = sr.ReadLine()) == null) 
       { 
        sr.Close(); 
        fs.Close(); 
        cond = false; 
        if (count == 0) 
         return false; 
       } 
       if (count == id) 
       { 
        string[] stringSplit = temp.Trim('\"').Split(new 
        String[] { "," }, StringSplitOptions.None); 
        //string[] stringSplit = temp.Split(','); 
        int _maxIndex = stringSplit.Length; 
        name = stringSplit[0];      
       } 
       count++; 
      } 
      sr.Close(); 
      fs.Close(); 
      return true; 
     } 
     catch 
     { 
      return false; 
     } 
    }      
+0

什么是预期的输出 –

+0

向后:temp.Split(新的String [] { “”}, StringSplitOptions.None)。选择(X => x.Trim( '\“')) .ToArray(); – jdweng

+0

我想显示数据到我的窗体,组成文本框,datetimepicker,combobox.I在阅读datetimepicker的字段时遇到错误。它是由于csv文件的格式是用双引号括起来,而在我有一个自定义格式的datetimepicker我设置了 – Sam

回答

0
The problem is not on that function. It is by other function and I used trim 
for it.                
string[] stringSplit = temp.Split(','); 
int _maxIndex = stringSplit.Length; 
name = stringSplit[0].Trim('"'); 
lastname = stringSplit[1].Trim('"'); 
phone = stringSplit[2].Trim('"'); 
mail = stringSplit[3].Trim('"'); 
website = stringSplit[4].Trim('"'); 
1

如果没有逗号引号的数据,例如一部分

"12,34","56","a""bc" -> 12,34 56 a"bc 

,你可以把一个简单的的Linq

string[][] result = File 
    .ReadLines(@"C"\MyData.csv") 
    .Select(line => line 
     .Split(',') 
     .Select(item => item.Trim('"')) 
     .ToArray()) 
    .ToArray(); 

进一步改善是返回一个定制类的数组:

MyClass[] result = File 
    .ReadLines(@"C"\MyData.csv") 
    .Select(line => line 
     .Split(',')) 
    .Select(items => new MyClass() { 
     Id = items[0].Trim('"'), 
     Date = DateTime.ParseExact(items[1].Trim('"') + " " + items[2].Trim('"'), 
           "MM-dd-yyyy hh:mm", 
           CultureInfo.InvariantCulture), 
     Code = items[3].Trim('"'), 
     Text = items[4].Trim('"'), 
    }) 
    .ToArray(); 
+0

当我使用ReadLines命令时,我遇到了错误的文件被其他应用程序使用 – Sam

+0

@Sam:检查你的代码:看起来你不关闭文件另一种可能是该文件是可打开的由MS Excel或其他东西打开它写作(因此想要exlusive访问) –

+0

好吧。我有你的想法有关修剪。我现在工作。感谢您的帮助。 – Sam

0

试试这个简单的功能。它将处理双引号和双引号之间的逗号。

private string[] GetCommaSeperatedWords(string sep, string line) 
{ 
    List<string> list = new List<string>(); 
    StringBuilder word = new StringBuilder(); 
    int doubleQuoteCount = 0; 
    for (int i = 0; i < line.Length; i++) 
    { 
     string chr = line[i].ToString(); 
     if (chr == "\"") 
     { 
      if (doubleQuoteCount == 0) 
       doubleQuoteCount++; 
      else 
       doubleQuoteCount--; 

      continue; 
     } 
     if (chr == sep && doubleQuoteCount == 0) 
     { 
      list.Add(word.ToString()); 
      word = new StringBuilder(); 
      continue; 
     } 
     word.Append(chr); 
    } 

    list.Add(word.ToString()); 

    return list.ToArray(); 
} 
相关问题