导入字段中包含多个值的 CSV 文件

发布于 2024-11-29 00:49:45 字数 1055 浏览 6 评论 0原文

这是我尝试从 CSV 导入的数据

Time|Person|Products|Address

(now)|person1|val1*val2*val3|adr1

,其中 * 是换行符/回车符

这是我导入数据时的样子: >

(now)|person1|val1val2val3|adr1 

这就是我需要的样子:

(now)|person1|val1|adr1

(now)|person1|val2|adr1 etc.

我尝试过:主要基于 string.Spilt() 自定义编码 CSV 解析器,它给了我确切的结果,花了很长时间来处理。我尝试过 Sebastien Lorion 的 CSVReader,它给了我完全相同的结果,尽管插入到我现有的代码中明显更快、更容易。我什至尝试过 VB TextFieldParser ,结果完全相同。

现在,这就是我想做的事情:

当我到达另一个返回 List 对象的方法时,以某种方式传递字段的值(我不知道如何引用它们)我可以对其进行迭代并将其添加到数据表中,然后从那里执行需要执行的操作。用一把旧叉子取出我的脑干。我一边走一边小声嘀咕着。

这是 TextFieldParser 代码:

OpenFileDialog op = new OpenFileDialog();
op.ShowDialog();
TextFieldParser parser = new TextFieldParser(new StreamReader(op.FileName));
parser.SetDelimiters(",");

while (!parser.EndOfData)
{
    contents.AddRange(parser.ReadFields());
}

Here's the data I'm trying to import from a CSV

Time|Person|Products|Address

(now)|person1|val1*val2*val3|adr1

where * is a linebreak/carriage return

here's what it looks like when I import it:

(now)|person1|val1val2val3|adr1 

Here's what I need it to look like:

(now)|person1|val1|adr1

(now)|person1|val2|adr1 etc.

I've tried: custom coding a CSV parser based mainly around string.Spilt(), it gave me the exact result and took forever to process. I've tried Sebastien Lorion's CSVReader and it gives me the exact same result, though it was markedly faster and easier to insert into my existing code. I've even tried the VB TextFieldParser with the exact same result.

Now, here's what I've thought about doing :

Somehow pass the field's values(I've no idea how to reference them) when I reach it to another method which returns a List<string> object through which I can iterate and add to a DataTable and from there do what needs doing. Remove my brain-stem with an old fork. Pace around muttering under my breath.

Here's the TextFieldParser code:

OpenFileDialog op = new OpenFileDialog();
op.ShowDialog();
TextFieldParser parser = new TextFieldParser(new StreamReader(op.FileName));
parser.SetDelimiters(",");

while (!parser.EndOfData)
{
    contents.AddRange(parser.ReadFields());
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

柏拉图鍀咏恒 2024-12-06 00:49:45

在我看来,如果您必须坚持使用这种 CSV 格式,我会像您上面所做的那样将其全部读入;但之后,我会循环遍历您的数据集合,然后手动将产品字符串拆分为您需要的组件。

试图一次性完成所有工作会使问题变得过于复杂。

一旦您获得了单独的类,您就可以在回车符上拆分产品,如下所示:

string[] result = input.Split(new string[] {"\n", "\r\n"}, StringSplitOptions.RemoveEmptyEntries);

It would seem to me that if you have to stick with this CSV format, I would read it all in as you're doing above; but afterwards, I'd loop through your collection of data and then manually string split the products into the components that you need.

Trying to do it all in one pass is over complicating the issue.

Once you've got your separate classes you can then split the product on Carriage returns like so:

string[] result = input.Split(new string[] {"\n", "\r\n"}, StringSplitOptions.RemoveEmptyEntries);
万人眼中万个我 2024-12-06 00:49:45

查看 FileHelpers 库。它非常快而且非常灵活。尽管我认为您必须为您想做的事情编写一些自定义处理逻辑。

Have a look at the FileHelpers library. It's very fast and quite flexible. Although I think you will have to write a little bit of custom processing logic for what you want to do.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文