解析用C#一个CSV文件,忽略千位分隔符 [英] Parsing a CSV File with C#, ignoring thousand separators

查看:474
本文介绍了解析用C#一个CSV文件,忽略千位分隔符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上的程序,需要一个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文件中,有四个单元,使用值狗,猫,十万,鱼。当我在,分裂字符串数组,它包含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代码的时候做了两个常见的错误:使用斯普利特()函数,并使用正则表达式。这两种方法都是错误的,因为他们很容易的角落情况下,如你的,慢于他们可以。

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 ,或我自己的FPGA实现这里对堆栈溢出。

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆