在 C# 中读取制表符分隔的文本文件的最佳方法是什么
我们有一个大约 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我会将其作为带有制表符列分隔符的 CSV 格式读取:
A Fast CSV Reader
编辑:
以下是您需要的简单示例:
其中 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:
Where CSV_FULLNAME is the full path + filename of your tab delimited CSV.
使用.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
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.
两个选项:
Two options:
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.无论您如何解析这些行,请确保使用支持转发和倒带的内容,作为数据网格的数据源。您不想先将所有内容加载到内存中,对吗?如果下次数据量增加十倍怎么样?制作一些深入使用 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.
简单,但不一定是好方法:
使用文本阅读器将文件读取为字符串
使用 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