NS字典。如何直接从格式化文件创建?

发布于 2024-08-04 06:26:03 字数 866 浏览 5 评论 0原文

这是一个相当简单的数据解析问题。我只是不清楚应该使用什么方法来实现它。

我有一个几百行的纯文本文件。每行的格式完全相同。这些行位于连续的块中,其中行中的第一项本质上是一个块中的每行重复的键:

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 技术交流群。

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

发布评论

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

评论(2

水水月牙 2024-08-11 06:26:04

以下解决方案提供了一个数组字典,但您可以使用它作为生成您喜欢的任何数据结构的基础:

// The values on each line are tab-delimited
NSString* data = @""
  "key1 a   b"
  "key1 c   d"
  "key2 e   f"
  "key2 g   h";

NSMutableDictionary* result = [NSMutableDictionary dictionary];  
NSArray* lines = [data componentsSeparatedByString:@"\n"];

for (NSString* line in lines) {
  NSArray* value = [line componentsSeparatedByString:@"\t"];
  NSString* key = [components objectWithIndex:0];
  [value removeObjectAtIndex:0]; // remove the key

  NSArray* currentValue = [result objectForKey:key];
  if (currentValue) {
    [currentValue addObjectsFromArray:value];
  } else {
    [result setObject:[value mutableCopy] forKey:key];
  }
}

/*

The result looks something like this:

{
  "key1": [
    ["a", "b"],
    ["c", "d"]
  ],
  "key2": [
    ["e", "f"],
    ["g", "h"]
  ]
}

*/

The following solution provides a dictionary of arrays, but you can use it as a basis for producing any data structure you like:

// The values on each line are tab-delimited
NSString* data = @""
  "key1 a   b"
  "key1 c   d"
  "key2 e   f"
  "key2 g   h";

NSMutableDictionary* result = [NSMutableDictionary dictionary];  
NSArray* lines = [data componentsSeparatedByString:@"\n"];

for (NSString* line in lines) {
  NSArray* value = [line componentsSeparatedByString:@"\t"];
  NSString* key = [components objectWithIndex:0];
  [value removeObjectAtIndex:0]; // remove the key

  NSArray* currentValue = [result objectForKey:key];
  if (currentValue) {
    [currentValue addObjectsFromArray:value];
  } else {
    [result setObject:[value mutableCopy] forKey:key];
  }
}

/*

The result looks something like this:

{
  "key1": [
    ["a", "b"],
    ["c", "d"]
  ],
  "key2": [
    ["e", "f"],
    ["g", "h"]
  ]
}

*/
柳若烟 2024-08-11 06:26:04

看一下 NSScanner 类。

Take a look at the NSScanner class.

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