在 C# 中读取制表符分隔的文本文件的最佳方法是什么

发布于 2024-08-19 03:32:58 字数 129 浏览 3 评论 0原文

我们有一个大约 100,000 行的文本文件,每行大约 50 列,大部分数据都非常小(5 到 10 个字符或数字)。

这是一个非常简单的任务,但只是想知道将此数据导入 C# 数据结构(例如 DataTable)的最佳方法是什么?

We have a text file with about 100,000 rows, about 50 columns per row, most of the data is pretty small (5 to 10 characters or numbers).

This is a pretty simple task, but just wondering what the best way would be to import this data into a C# data structure (for example a DataTable)?

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

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

发布评论

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

评论(6

机场等船 2024-08-26 03:32:58

我会将其作为带有制表符列分隔符的 CSV 格式读取:

A Fast CSV Reader

编辑:
以下是您需要的简单示例:

DataTable dt = new DataTable();
using (CsvReader csv = new CsvReader(new StreamReader(CSV_FULLNAME), false, '\t')) {
    dt.Load(csv);
}

其中 CSV_FULLNAME 是制表符分隔的 CSV 的完整路径 + 文件名。

I would read it in as a CSV with the tab column delimiters:

A Fast CSV Reader

Edit:
Here's a barebones example of what you'd need:

DataTable dt = new DataTable();
using (CsvReader csv = new CsvReader(new StreamReader(CSV_FULLNAME), false, '\t')) {
    dt.Load(csv);
}

Where CSV_FULLNAME is the full path + filename of your tab delimited CSV.

2024-08-26 03:32:58

使用.NET 的内置文本解析器。它是免费的,具有出色的错误处理能力,并且可以处理很多奇怪的情况。

http://msdn.microsoft .com/en-us/library/microsoft.visualbasic.fileio.textfieldparser(VS.80).aspx

Use .NET's built in text parser. It is free, has great error handling, and deals with a lot of odd ball cases.

http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.fileio.textfieldparser(VS.80).aspx

独孤求败 2024-08-26 03:32:58

FileHelpers 怎么样,您可以将制表符定义为分隔符。通过提供的链接前往该网站并查看一下。

希望这有帮助,
此致,
汤姆.

What about FileHelpers, you can define the tab as a delimiter. HEad on over to that site by the link supplied and have a peeksy.

Hope this helps,
Best regards,
Tom.

傾旎 2024-08-26 03:32:58

两个选项:

  1. 使用 System.Data.OleDb 命名空间中的类。这样做的优点是可以像您所要求的那样直接读取数据表,只需很少的代码,但要正确读取数据可能很困难,因为它是制表符而不是逗号分隔的。
  2. 使用或编写 csv 解析器。确保它是一个基于状态机的解析器,例如链接到的 @Jay Riggs,而不是基于 String.Split() 的解析器。这应该比 OleDb 方法更快,但它会给您一个列表或数组而不是数据表。

Two options:

  1. Use the classes in the System.Data.OleDb namespace. This has the advantage of reading directly into a datatable like you asked with very little code, but it can be tricky to get right because it's tab rather than comma delimited.
  2. Use or write a csv parser. Make sure it's a state machine-based parser like the one @Jay Riggs linked to rather than a String.Split()-based parser. This should be faster than the OleDb method, but it will give you a List or array rather than a datatable.
凝望流年 2024-08-26 03:32:58

无论您如何解析这些行,请确保使用支持转发和倒带的内容,作为数据网格的数据源。您不想先将所有内容加载到内存中,对吗?如果下次数据量增加十倍怎么样?制作一些深入使用 file.seek 的东西,不要先将所有内容读取到内存中。这是我的建议。

However you parse the lines, make sure you use something that supports forwarding and rewinding, being the data source of your data grid. You don't want to load everything into memory first, do you? How about if the amount of data should be ten-fold the next time? Make something that uses file.seek deep down, don't read everything to memory first. That's my advice.

泪冰清 2024-08-26 03:32:58

简单,但不一定是好方法:

  • 使用文本阅读器将文件读取为字符串

  • 使用 String.Split 获取行

    p>

  • 使用带有制表符的 String.Split 获取字段值

Simple, but not the necessarily a great way:

  • Read the file using a text reader into a string

  • Use String.Split to get the rows

  • use String.Split with a tab character to get field values

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