首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从文本文件中获取特定信息

从文本文件中获取特定信息
EN

Stack Overflow用户
提问于 2013-08-16 21:13:33
回答 2查看 158关注 0票数 0

我有一个~7mb的文本文件,我想从中提取一些信息,它包含许多格式类似的实例:

代码语言:javascript
复制
            "name": "Riki's Dagger",
            "defindex": 0,
            "item_class": "dota_item_wearable",
            "item_type_name": "#DOTA_WearableType_Daggers",
            "item_name": "#DOTA_Item_Rikis_Dagger",
            "proper_name": false,
            "item_quality": 0,
            "image_inventory": null,
            "min_ilevel": 1,
            "max_ilevel": 1,
            "image_url": "",
            "image_url_large": "",

我想提取名称和定义,检查这个实例是否包含一些关键字,然后把它放在一个新的文本文件中,以便我以后可以使用它。我的计划是在文件中搜索"name“的每个实例(带引号),并将"name”下一个实例之前的所有内容设置为一个名为current的变量。然后在当前字符串中搜索我需要的信息。这是最好的方法吗?我该怎么做?我应该使用正则表达式还是文件太大?如果能有一些方向,我们将非常感激。

这就是我到目前为止所知道的:

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.IO;

namespace ConsoleApplication1
{
    class Test
    {
        static void Main(string[] args)
            {
            string ingameschemaFilePath = @"C:\Users\Andrew\Documents\GitHub\SteamBot\Bin\Debug\ingameschema.txt";
            string dota2schemaFilePath = @"C:\Users\Andrew\Documents\GitHub\SteamBot\Bin\Debug\dota2schema.txt";
            string schemaFilePath = @"C:\Users\Andrew\Documents\GitHub\SteamBot\Bin\Debug\schema.txt";

            string[] ingameschema = File.ReadAllLines(ingameschemaFilePath);
            string[] dota2schema = File.ReadAllLines(dota2schemaFilePath);
            string[] current = null;
            string[] name = null;
            string[] defindex = null;
            string[] rarity = null;

            using (TextWriter textWriter = new StreamWriter(schemaFilePath))
            {
                foreach (//search for "name"->"name" segment here)
                {
                    //    if current.Contains("dota_item_wearable") == false, current.Contains("announcer", "courier", "ward", "egg", "costume", "HUD", "smeevil", "taunt", "bait", "lure", "bundle" ) == true, 
                    //          break
                    }
                }
            System.Console.WriteLine("Press any key to exit.");
            System.Console.ReadKey();
    }
    }
}
EN

回答 2

Stack Overflow用户

发布于 2013-08-16 21:24:04

我认为您应该使用StreamReader从文本文件中逐行读取,然后在该行中找到所需的信息。

只有一个问题,如果你在读完文件之前存储文件的一部分,那么你可能会遇到内存问题(但你会惊讶地发现,你可以让列表和字典在内存耗尽之前变得如此之大)

您需要做的是尽快保存处理后的数据,而不是将其保存在内存中(或尽可能少地保存在内存中)。

票数 0
EN

Stack Overflow用户

发布于 2013-08-16 21:41:42

您可以考虑的一种方法是将源代码放入某种基于字典的集合中,然后您可以通过您对该项目感兴趣的关键字来寻址。

示例

代码语言:javascript
复制
    static void Main(string[] args)
    {
        string sourcefile = @"C:\test\source.txt";
        string outputfile = @"C:\test\output.txt";

        string[] source = File.ReadAllLines(sourcefile);

        // The list would represent the collection of all the items 
        List<NameValueCollection> list = new List<NameValueCollection>();

        // Each nvc would represent the collection of attributes for that item
        NameValueCollection nvc = null;

        foreach (string s in source)
        {
            //Split your string into its key and value
            string[] nv = s.Split(':');

            //If the key is name you have finished your previous item, and will it to the list and start a new one
            if (nv[0] == "name")
            {
                if (nvc != null)
                    list.Add(nvc);

                nvc = new NameValueCollection();
            }
            // Add your attribute and value to the items attribute collection
            nvc.Add(nv[0], nv[1]);
        } 
    }

7mb有点大,但以今天的内存来说,应该没问题。如果这成为一个问题,你可以考虑使用来自Stream对象的ReadLine,它会一次加载一行到内存中。

如果这有任何帮助,请让我知道。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18274179

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档