使用C#解析CSV文件,忽略千分位数 [英] Parsing a CSV File with C#, ignoring thousand separators
问题描述
使用一个包含CSV文件并在每个,分隔的程序。我有的问题是在一些数字有千分离器。在CSV文件中,数字正确显示。当查看为文本文档时,它们显示如下:
Working on a program that takes a CSV file and splits on each ",". The issue I have is there are thousand separators in some of the numbers. In the CSV file, the numbers render correctly. When viewed as a text document, they are shown like below:
狗,猫,100,100,鱼
Dog,Cat,100,100,Fish
在CSV文件中,有四个单元格,其值为Dog,Cat,100,000,Fish。当我把,分割成一个字符串数组时,它包含5个元素,当我想要的是4.任何人都知道一种方法来解决这个问题。
In a CSV file, there are four cells, with the values "Dog", "Cat", "100,000", "Fish". When I split on the "," to an array of strings, it contains 5 elements, when what I want is 4. Anyone know a way to work around this?
感谢
推荐答案
在读取csv代码时有两个常见的错误:使用split()函数和使用正则表达式。这两种方法都是错误的,因为它们容易出现问题,比如你的和比他们可能更慢。
There are two common mistakes made when reading csv code: using a split() function and using regular expressions. Both approaches are wrong, in that they are prone to corner cases such as yours and slower than they could be.
而是使用一个专用的解析器,如Microsoft.VisualBasic。 TextFieldParser,CodeProject的 FastCSV 或 Linq2csv 或我自己的实现在堆栈溢出。
Instead, use a dedicated parser such as Microsoft.VisualBasic.TextFieldParser, CodeProject's FastCSV or Linq2csv, or my own implemention here on Stack Overflow.
这篇关于使用C#解析CSV文件,忽略千分位数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!