I need help parsing this example text:
我需要帮助解析此示例文本:
[item1]
2550 coins 995 200000 7
2550 coins 995 200000 7
2550 coins 995 200000 7
2550 coins 995 200000 7
2550 coins 995 200000 7
[/item1]
[item2]
2550 coins 995 200000 7
2550 coins 995 200000 7
2550 coins 995 200000 7
2550 coins 995 200000
2550 coins 995 200000 7
[/item2]
[item3]
2550 coins 995 200000 7
2550 coins 995 200000 7
2550 coins 995 200000 7
2550 coins 995 200000 7
2550 coins 995 200000 7
[/item3]
I have no idea how to read the data from each item while keeping the item heading(item1 etc..) associated with data. I am sorry but I have no idea how to approach this, so far this is what I have got for parsing one item without the headings.
我不知道如何从每个项目中读取数据,同时保持项目标题(item1等..)与数据相关联。我很抱歉,但我不知道如何处理这个问题,到目前为止,这是解析一个没有标题的项目的原因。
private void button1_Click(object sender, EventArgs e)
{
//dataGridView1.Rows.Add("Ashes", "d", "a", "g");
//dataGridView1.Rows[0].Cells[3].Style.BackColor = Color.Aqua;
String fileName = "Drops.de";
StreamReader streamReader = new StreamReader(fileName);
int[] nums = new int[4];
int npcID;
int itemID;
int itemAmount;
int itemRarity;
string itemName;
// string currentLine = streamReader.ReadLine();
while (!streamReader.EndOfStream)
{
string currentLine = streamReader.ReadLine();
if (!currentLine.Contains('#') && currentLine != "" && !currentLine.Contains("["))
{
String[] s = currentLine.Split(' ');
npcID = int.Parse(s[0]);
itemName = (s[1]);
itemID = int.Parse(s[2]);
itemAmount = int.Parse(s[3]);
itemRarity = int.Parse(s[4]);
dataGridView1.Rows.Add(itemName, itemID, itemAmount, itemRarity);
dataGridView1.Refresh();
}
else
{
streamReader.ReadLine();
}
}
streamReader.Close();
}
Any and all help would be greatly appreciated :)
任何和所有的帮助将不胜感激:)
2 个解决方案
#1
0
you already have a branch for reading lines with the [item] identifyer ... use it to read the desired info about the item ... store that info in a variable outside of your loop ... everytime you read a line without the [item] identifyer, you can still use that variable to know what item this line belongs to ...
你已经有一个分支用于读取[item]标识符的行...用它来读取有关该项目的所需信息...将该信息存储在循环外的变量中...每次你读取一行没有[item]标识符,您仍然可以使用该变量来了解此行所属的项目...
string headingItem="unknown Item";
while (!streamReader.EndOfStream)
{
string currentLine = streamReader.ReadLine();
if (!currentLine.Contains('#') && currentLine != "" && !currentLine.Contains("["))
{
String[] s = currentLine.Split(' ');
npcID = int.Parse(s[0]);
itemName = (s[1]);
itemID = int.Parse(s[2]);
itemAmount = int.Parse(s[3]);
itemRarity = int.Parse(s[4]);
dataGridView1.Rows.Add(headingItem, itemName, itemID, itemAmount, itemRarity);
dataGridView1.Refresh();
}
else
{
headingItem=currentLine;
}
}
#2
0
this doesn't required u are reading it second time something wrong here
这不要求你在第二次读错了
else
{
streamReader.ReadLine();
}
in the case of item2 ->> 4th record you may got exception index out bound array at itemRarity = int.Parse(s[4]);
在item2 - >> 4th record的情况下,你可以在itemRarity = int.Parse(s [4])得到异常索引输出绑定数组;
cause there is no fifth element
因为没有第五个元素
the best you can do is at if condition
你能做的最好的就是条件
if(Regex.match(currentline,@"^\d+\s\w+\s\d+\s\d+\s\d+\s*?$"))
{
#now do your logic here
}
Here is Complete Code:
这是完整代码:
private void button1_Click(object sender, EventArgs e)
{
String fileName = "d:\\Drops.txt";
StreamReader streamReader = new StreamReader(fileName);
dataGridView1.Rows.Clear();
dataGridView1.Columns.Clear();
dataGridView1.Columns.Add("Head","Head");
dataGridView1.Columns.Add("npcID", "npcID");
dataGridView1.Columns.Add("itemName", "itemName");
dataGridView1.Columns.Add("itemID", "itemID");
dataGridView1.Columns.Add("itemAmount", "itemAmount");
dataGridView1.Columns.Add("itemRarity", "itemRarity");
string itemhead = "Not Found";
while (!streamReader.EndOfStream)
{
string currentLine = streamReader.ReadLine();
if (Regex.IsMatch(currentLine, @"^\d+\s\w+\s\d+\s\d+\s\d+?\s*?$"))
{
List<string> s = new List<string>();
s.Add(itemhead);
s.AddRange(currentLine.Split(' '));
dataGridView1.Rows.Add(s.ToArray());
dataGridView1.Refresh();
}
else if(Regex.IsMatch(currentLine,@"\[[^/]*\]"))
{
itemhead = Regex.Match(currentLine, @"\[([^/]*)\]").Groups[0].Value;
}
}
streamReader.Close();
}
#1
0
you already have a branch for reading lines with the [item] identifyer ... use it to read the desired info about the item ... store that info in a variable outside of your loop ... everytime you read a line without the [item] identifyer, you can still use that variable to know what item this line belongs to ...
你已经有一个分支用于读取[item]标识符的行...用它来读取有关该项目的所需信息...将该信息存储在循环外的变量中...每次你读取一行没有[item]标识符,您仍然可以使用该变量来了解此行所属的项目...
string headingItem="unknown Item";
while (!streamReader.EndOfStream)
{
string currentLine = streamReader.ReadLine();
if (!currentLine.Contains('#') && currentLine != "" && !currentLine.Contains("["))
{
String[] s = currentLine.Split(' ');
npcID = int.Parse(s[0]);
itemName = (s[1]);
itemID = int.Parse(s[2]);
itemAmount = int.Parse(s[3]);
itemRarity = int.Parse(s[4]);
dataGridView1.Rows.Add(headingItem, itemName, itemID, itemAmount, itemRarity);
dataGridView1.Refresh();
}
else
{
headingItem=currentLine;
}
}
#2
0
this doesn't required u are reading it second time something wrong here
这不要求你在第二次读错了
else
{
streamReader.ReadLine();
}
in the case of item2 ->> 4th record you may got exception index out bound array at itemRarity = int.Parse(s[4]);
在item2 - >> 4th record的情况下,你可以在itemRarity = int.Parse(s [4])得到异常索引输出绑定数组;
cause there is no fifth element
因为没有第五个元素
the best you can do is at if condition
你能做的最好的就是条件
if(Regex.match(currentline,@"^\d+\s\w+\s\d+\s\d+\s\d+\s*?$"))
{
#now do your logic here
}
Here is Complete Code:
这是完整代码:
private void button1_Click(object sender, EventArgs e)
{
String fileName = "d:\\Drops.txt";
StreamReader streamReader = new StreamReader(fileName);
dataGridView1.Rows.Clear();
dataGridView1.Columns.Clear();
dataGridView1.Columns.Add("Head","Head");
dataGridView1.Columns.Add("npcID", "npcID");
dataGridView1.Columns.Add("itemName", "itemName");
dataGridView1.Columns.Add("itemID", "itemID");
dataGridView1.Columns.Add("itemAmount", "itemAmount");
dataGridView1.Columns.Add("itemRarity", "itemRarity");
string itemhead = "Not Found";
while (!streamReader.EndOfStream)
{
string currentLine = streamReader.ReadLine();
if (Regex.IsMatch(currentLine, @"^\d+\s\w+\s\d+\s\d+\s\d+?\s*?$"))
{
List<string> s = new List<string>();
s.Add(itemhead);
s.AddRange(currentLine.Split(' '));
dataGridView1.Rows.Add(s.ToArray());
dataGridView1.Refresh();
}
else if(Regex.IsMatch(currentLine,@"\[[^/]*\]"))
{
itemhead = Regex.Match(currentLine, @"\[([^/]*)\]").Groups[0].Value;
}
}
streamReader.Close();
}