解析在C#中的文本文件以跳过一些内容 [英] Parsing a textfile in C# with skipping some contents

查看:143
本文介绍了解析在C#中的文本文件以跳过一些内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图解析,有一个标题和身体的文本文件。在这个文件的标题中,有行号的引用到身体的部分。例如:

I'm trying to parse a text file that has a heading and the body. In the heading of this file, there are line number references to sections of the body. For example:

SECTION_A 256
SECTION_B 344
SECTION_C 556

这意味着,SECTION_A开始于行256。

This means, that SECTION_A starts in line 256.

什么是本项解析为一个字典,然后在需要时阅读部分的最佳方式。

What would be the best way to parse this heading into a dictionary and then when necessary read the sections.

典型的场景是:

  1. 解析头和只读部分SECTION_B
  2. 解析头和阅读每一个章节的拳头段落。

数据文件是相当大的,我绝对不希望加载所有的一切到内存中,然后就可以进行操作。

The data file is quite large and I definitely don't want to load all of it to the memory and then operate on it.

我倒是AP preciate您的建议。我的环境是VS 2008和C#3.5 SP1。

I'd appreciate your suggestions. My environment is VS 2008 and C# 3.5 SP1.

推荐答案

显然,你可以存储姓名+行号成一个字典,但是这不会给你带来任何好处。

Well, obviously you can store the name + line number into a dictionary, but that's not going to do you any good.

嗯,肯定的是,它可以让你知道,开始从阅读哪一行,但问题是,在该文件中是行?要知道,唯一的办法就是从头开始,并开始计数。

Well, sure, it will allow you to know which line to start reading from, but the problem is, where in the file is that line? The only way to know is to start from the beginning and start counting.

最好的办法是写一个包装,德codeS文本内容(如果您有编码的问题),并且可以给你一个行号映射的字节位置的类型,那么你可以采取的行号, 256,并期待在字典中知道该行的文件中启动256位10000,并从那里开始阅读。

The best way would be to write a wrapper that decodes the text contents (if you have encoding issues) and can give you a line number to byte position type of mapping, then you could take that line number, 256, and look in a dictionary to know that line 256 starts at position 10000 in the file, and start reading from there.

这是一个一次性的处理情况如何?如果没有,你有没有考虑将整个文件塞进一个本地数据库,如SQLite数据库?这将让你有行号和它的内容之间的直接映射。当然,这个文件会比原文件更大,而且你需要从文本文件到数据库中的数据拷贝,所以有一定的开销两种方式。

Is this a one-off processing situation? If not, have you considered stuffing the entire file into a local database, like a SQLite database? That would allow you to have a direct mapping between line number and its contents. Of course, that file would be even bigger than your original file, and you'd need to copy data from the text file to the database, so there's some overhead either way.

这篇关于解析在C#中的文本文件以跳过一些内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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