0

我正在尝试阅读 csv

以下是示例。

"0734306547          ","9780734306548       ","Jane Eyre Pink PP                       ","Bronte Charlotte                        ","FRONT LIST",20/03/2013 0:00:00,0,"PAPERBACK","Y","Pen"

这是我正在使用读取 CSV 的代码

public void readCSV()
        {
            StreamReader reader = new StreamReader(File.OpenRead(@"C:\abc\21-08-2013\PNZdatafeed.csv"),Encoding.ASCII);
            List<string> ISBN = new List<String>();

            while (!reader.EndOfStream)
            {
                string line = reader.ReadLine();
                if (!String.IsNullOrWhiteSpace(line))
                {
                    string[] values = line.Split(',');
                    if (values[9] == "Pen")
                    {
                        ISBN.Add(values[1]);
                    }
                }
            }
            MessageBox.Show(ISBN.Count().ToString());

        }

我无法比较它的值if (values[9] == "Pen"),因为当我调试代码时它说values[9]值是\"Pen\""

我如何摆脱特殊字符。?

4

1 回答 1

1

这里的问题是,每次找到,并留下这样的数据时,您都会拆分线路。例如,如果这是您正在阅读的行:

 "A","B","C"

你用逗号分割它,你会得到"A", "B", 和"C"作为你的数据。根据您的描述,您不希望在数据周围加上引号。

要丢弃字符串周围的引号:

  1. 检查最左边的字符是否为".
  2. 如果是,请检查最右边的字符是否为".
  3. 如果是这样,请删除最左边和最右边的字符。

In pseudocode:

 if (data.left(1) == "\"" && data.right(1) == "\"") {
      data = data.trimleft(1).trimright(1)
 }

At this point you might have a few questions (I'm not sure how much experience you have). If any of these apply to you, feel free to ask them, and I'll explain further.

  1. What does "\"" mean?
  2. How do I extract the leftmost/rightmost character of a string?
  3. How do I extract the middle of a string?
于 2013-08-21T21:54:44.623 回答