CSV文件解析方法,它将001读取为001,而不是1 [英] CSV file parse Method which will read 001 as 001 instead of 1

查看:111
本文介绍了CSV文件解析方法,它将001读取为001,而不是1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码来解析CSV文件

I have below code to parse CSV file

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Odbc;
using System.IO;
using System.Data.OleDb;
using System.Globalization;

namespace ReadCSVFile
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string path =@"D:\csvfile.csv";
            DataTable de = GetDataTableFromCsv(path, false);
        }

        static DataTable GetDataTableFromCsv(string path, bool isFirstRowHeader)
        {
            string header = isFirstRowHeader ? "Yes" : "No";

            string pathOnly = Path.GetDirectoryName(path);
            string fileName = Path.GetFileName(path);

            string sql = @"SELECT * FROM [" + fileName + "]";

            using (OleDbConnection connection = new OleDbConnection(
                      @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + pathOnly +
                      ";Extended Properties=\"Text;HDR=" + header + "\""))
            using (OleDbCommand command = new OleDbCommand(sql, connection))
            using (OleDbDataAdapter adapter = new OleDbDataAdapter(command))
            {
                DataTable dataTable = new DataTable();
                dataTable.Locale = CultureInfo.CurrentCulture;
                adapter.Fill(dataTable);
                return dataTable;
            }
        }


    }
}




它适用于字母数字数据
赞如果我们下面有CSV数据
Member1,BL,是
Member2,BL,否
Member3,BL,是

然后,GetDataTableFromCsv检索表中的正确数据,如

Member1 BL
Member2 BL
成员3 BL


但是,如果我们有以下CSV数据
001,BL,是
002,BL,No
003,BL,是

然后,GetDataTableFromCsv不会在表中返回正确的数据,如

1 BL
2 BL
3 BL



我希望返回数据采用以下格式

001 BL
002 BL
003 BL


总之,我希望上述CSV解析方法将memberid列视为字符串数据类型,而不是数字数据类型,以便它将按原样(使用00)进行解析


在此先感谢
--Rahul D.




It works well with alphanumeric data
Like If we have below CSV data
Member1,BL,Yes
Member2,BL,No
Member3,BL,Yes

Then GetDataTableFromCsv retuen proper data in table like

Member1BLYes
Member2BLNo
Member3BLYes


ButIf we have below CSV data
001,BL,Yes
002,BL,No
003,BL,Yes

Then GetDataTableFromCsv does not return proper data in table like

1BLYes
2BLNo
3BLYes



I want return data to be in below format

001BLYes
002BLNo
003BLYes


In summary I want the above CSV parsing method consider memberid column as string datatype instead of numeric data type so that It will parse it as it is (with 00)


Thanks in Advance
--Rahul D.

推荐答案

这篇CSV文章很棒 [ ^ ],并根据MIT许可发布.
This CSV article is pretty awesome[^] and is released under MIT licence.


如果您想自己滚动,可以执行类似
的操作
If you want to roll your own, you can do something like
string[][] result;

string[] lines = content.Split(''\n'');
result = new string[lines.Length][];
for(int ri = 0; ri < lines.Length; ri++)
 result[ri] = lines[ri].Split('','');



但是请注意,这并没有实现完整的CSV标准.处理引用的项目,带有逗号的项目等非常棘手(这就是为什么有第三方库可以为您完成此工作的原因).

我敢肯定,您也可以找一个正则表达式来查找它.尽管正则表达式通常效率很低(但是,如果您的表确实是3×3,那么就没关系了.)



Be aware, though, that this does not implement the complete CSV standard. Handling quoted items, items with commas in etc is considerably trickier (which is why there are third party libraries available that do it for you).

I''m sure there is also a regex you could look up to do it. Regexes are typically quite inefficient, though (but if your tables are really 3×3 then that won''t matter).


看看
Take a look at this page[^], it may have the solution you need.


这篇关于CSV文件解析方法,它将001读取为001,而不是1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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