我有一个~7mb的文本文件,我想从中提取一些信息,它包含许多格式类似的实例:
"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的变量。然后在当前字符串中搜索我需要的信息。这是最好的方法吗?我该怎么做?我应该使用正则表达式还是文件太大?如果能有一些方向,我们将非常感激。
这就是我到目前为止所知道的:
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();
}
}
}发布于 2013-08-16 21:24:04
我认为您应该使用StreamReader从文本文件中逐行读取,然后在该行中找到所需的信息。
只有一个问题,如果你在读完文件之前存储文件的一部分,那么你可能会遇到内存问题(但你会惊讶地发现,你可以让列表和字典在内存耗尽之前变得如此之大)
您需要做的是尽快保存处理后的数据,而不是将其保存在内存中(或尽可能少地保存在内存中)。
发布于 2013-08-16 21:41:42
您可以考虑的一种方法是将源代码放入某种基于字典的集合中,然后您可以通过您对该项目感兴趣的关键字来寻址。
示例
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,它会一次加载一行到内存中。
如果这有任何帮助,请让我知道。
https://stackoverflow.com/questions/18274179
复制相似问题