读取csv文件并上传数据 [英] Reading csv file and uploading the data

查看:224
本文介绍了读取csv文件并上传数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我必须读取一个csv文件并将数据上传到数据表中,但问题是我有一个名为金额"的列,用户在其中提供了10,31,218.00这样的数据
因此,当从csv文件读取数据时,它会将特定数量的数据视为3个不同的列作为csv.
任何人都可以帮助我限制数量用逗号分隔的csv文件.
在此先谢谢您

Hi all,
I have to read a csv file and upload the data into a datatable but the issue is I have column called amount in which the user gives data like 10,31,218.00
So when am reading the data from csv file its considering that particular amount data as 3 different columns as its csv.
can anyone help me in restricting csv file where amount data is comma seperated.
Thanks in advance

推荐答案

您可以在CSV文件中更改定界符,或者在文件中添加某种转义符.您的C#代码永远无法确定逗号是定界符还是数字分隔符.
You could change the delimiter in CSV file or, add some sort of escape character in the file. Your C# code, could never be sure if the comma is delimiter or number separator.


CSV是一个非常没用的过时的概念.最好改用DSV(定界符分隔的值).您可以使用任何喜欢的分隔符(并同意)并使用反斜杠转义特殊字符.

如果仍然要使用csv,则可以同意小数点是点,数字之间用逗号分隔.另一个解决方案是将每个值都用引号引起来.

祝你好运!
CSV is a rather useless outdated concept to be honest. It would be better to use DSV (delimiter separated values) instead. You can just take any separator you like (and agree on) and escape special characters using a backslash.

If you still are going to use csv you can agree on the number decimal being a point and numbers separated using a comma. Another solution is to simply surround each value with quotes.

Good luck!


我对待这种情况的方式几乎没有什么不同.我会编写自己的文本处理器来读取我收到的非标准csvs.它非常简单,您始终可以使用正则表达式"阅读任何此类内容.

1.逐行使用C#读取文件.
2.使用RegEx
将记录分成几列
我将以小的示例工作代码进行解释:

The way I approach this scenario little differently. I would write my own text processor to read non standard csvs I recieve. It''s very simple and you can always read any such stuffs using "Regular Expressions"

1. Read the file using C# by line by line.
2. Split the records into columns using RegEx

I will explain with small sample working code:

Regex _Expression;
private StreamReader textReader;
private string CSVSourceFeedPath;

textReader = new StreamReader(CSVSourceFeedPath);
string _Statement = String.Format
        ("{0}(?=(?:[^{1}]*{1}[^{1}]*{1})*(?![^{1}]*{1}))",
                        Regex.Escape(";"), Regex.Escape("\"")); //define your delimeters i used semicolon ; here

RegexOptions _Options = RegexOptions.Compiled | RegexOptions.Multiline;
_Expression = new Regex(_Statement, _Options);

string nextLine;
string[] columns;



nextLine = textReader.ReadLine();
while (nextLine != null)
{
    columns = _Expression.Split(nextLine);
    if (columns.Length >= 20//your expected number of columns)
    {
//process here. columns[0] will be your first column
    }
nextLine = textReader.ReadLine();
}


这篇关于读取csv文件并上传数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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