NS字典。如何直接从格式化文件创建?
这是一个相当简单的数据解析问题。我只是不清楚应该使用什么方法来实现它。
我有一个几百行的纯文本文件。每行的格式完全相同。这些行位于连续的块中,其中行中的第一项本质上是一个块中的每行重复的键:
key0
key0
key0
...
keyN
keyN
keyN
我想直接从该文件构造一个 NSDictionarys,其中给定键的行被折叠到字典中。所以字典中的字典。
关于如何做到这一点有什么想法吗?
干杯, Doug
UPDATE 0 - 这是实际数据的快照 这是实际数据。我可以将文件切成每个字符的块吗?如果需要的话。我对摄取单个字符的解决方案感到满意。
chr1 0 2300000 p36.33 gneg
chr1 2300000 5300000 p36.32 gpos25
chr1 5300000 7100000 p36.31 gneg
chr1 7100000 9200000 p36.23 gpos25
// ... // 还有更多 // ...
chrN 144700000 148400000 q22.3 gpos100
chrN 148400000 149600000 q23.1 gneg
chrN 149600000 150300000 q23.2 gpos25
chrN 150300000 154600000 q2 3.3 gneg
更新 1 - 文件位于磁盘上 如果我没有说清楚,数据位于磁盘上而不是驻留在内存上。实际上,我认为我可以将文件切成碎片,每个字符一个。然后我可以摄取到 NSArray 中,然后摄取到 NSDictionary 中。当然,除非有人有更时髦的东西。
This is a fairly trivial data parsing question. I'm just unclear on the methods I should be using to pull it off.
I've got a plain text file of a few hundred lines. Each line is of exactly the same format. The lines are in contiguous chunks where the first item in a line is essentially a key that is repeated for each line in a chunk:
key0
key0
key0
...
keyN
keyN
keyN
I would like to construct an NSDictionarys directly from this file where the lines for a given key are collapsed into a dictionary. So a dictionary of dictionarys.
Any thoughts on how to do this?
Cheers,
Doug
UPDATE 0 - Here's a snapshot of the actual data
Here is the actual data. I can dice the file into chunks for each chr? if need be. I'm happy with a solution that ingests a single chr.
chr1 0 2300000 p36.33 gneg
chr1 2300000 5300000 p36.32 gpos25
chr1 5300000 7100000 p36.31 gneg
chr1 7100000 9200000 p36.23 gpos25
// ...
// lots more
// ...
chrN 144700000 148400000 q22.3 gpos100
chrN 148400000 149600000 q23.1 gneg
chrN 149600000 150300000 q23.2 gpos25
chrN 150300000 154600000 q23.3 gneg
UPDATE 1 - The File is on disk
In case I didn't make it clear, the data is on disk not memory resident. I actually think I could get away with dicing the file into pieces, one for each chr. I can then ingest into an NSArray and then on into an NSDictionary. Unless of course, someone has something snazzier.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
以下解决方案提供了一个数组字典,但您可以使用它作为生成您喜欢的任何数据结构的基础:
The following solution provides a dictionary of arrays, but you can use it as a basis for producing any data structure you like:
看一下
NSScanner
类。Take a look at the
NSScanner
class.