C#如何从文本文件中获取数据
How to get data's from text file in c#
我有一个包含大量数据的纺织品,首先我必须过滤散落在那里的叶细胞数据 here.For 我过滤的第一行是以 ADD GCELL
开头的行其中包含主要数据,接下来我要做的是我必须通过使用 CELLID
从相同的文本文件中获取相关数据 ADD GCELL
line.Related datas are coming in the以 ADD GTRX
开头的行和数据是 FREQ , TRXNO , ISMAINBCCH ,
。简而言之,CELLID
是行 ADD GCELL
和 ADD GTRX
的共同值。我在 c# 中完成了一些编码,但我被困在某个地方
这是文本文件的一部分
...................................
.........
ADD GCELL:CELLID=13, CELLNAME="NR_0702_07021_G1_A", MCC="424", MNC="02", LAC=6112, CI=7021, NCC=6, BCC=0, EXTTP=Normal_cell, IUOTP=Concentric_cell, ENIUO=ON, DBFREQBCCHIUO=Extra, FLEXMAIO=OFF, CSVSP=3, CSDSP=5, PSHPSP=4, PSLPSVP=6, BSPBCCHBLKS=1, BSPAGBLKSRES=4, BSPRACHBLKS=1, TYPE=GSM900_DCS1800, OPNAME="Tester", VIPCELL=NO
..............................
ADD GTRX:TRXID=11140, TRXNAME="T_RAK_JaziratHamra_G_702_7021_A-0", FREQ=99, TRXNO=0, CELLID=13, IDTYPE=BYID, ISMAINBCCH=YES, ISTMPTRX=NO, GTRXGROUPID=80;
我做的代码是
using (StreamReader sr = File.OpenText(filename))
{
while ((s = sr.ReadLine()) != null)
{
if (s.Contains("ADD GCELL:"))
{
s = s.Replace("ADD GCELL:", "");
string[] items = s.Split(',');
foreach (string str in items)
{
string[] str1 = str.Split('=');
if (str1[0] == "CELLID")
{
cellidnew = str1[1];
}
string fieldname = str1[0];
string value = str1[1].Replace(";", string.Empty).Replace("\"", string.Empty);
}
Getgtrxvalues(filename, ref cellname, ref cellidnew, ref Frequency, ref TRXNO ,ref ISMAINBCCH);
}
}
}
private static void Getgtrxvalues(string filename, ref string cellname, ref string cellid, ref int Frequency, ref int TRXNO ,ref bool ISMAINBCCH)
{
using (StreamReader sr = File.OpenText(filename))
{
while ((s = sr.ReadLine()) != null)
{
if (s.Contains("ADD GTRX:"))
{
try
{
}
}
}
}
更新
一切正常,除了我必须 satisfy.Here 添加 Gtrx 的另一个条件:当 ISMAINBCCH=YES 时,我采用包括 Freq 在内的所有值,但同时 ISMAINBCCH=NO 有 Freq 的值我必须得到逗号分隔的 values.For 示例 就像这里 首先我将采用 FREQ,其中 CELLID = 639(动态的任何事情都可能发生)和 ISMAINBCCH=YES,我现在完成的下一个任务是我必须连接 FREQ以逗号分隔的值,其中 CELLID=639 和 ISMAINBCCH=NO,所以这里我想要的输出是 24,28,67。如何实现这个
行是
ADD GTRX:TRXID=0, TRXNAME="M_RAK_JeerExch_G_1879_18791_A-0", FREQ=81, TRXNO=0, CELLID=639, IDTYPE=BYID, ISMAINBCCH=YES, ISTMPTRX=NO, GTRXGROUPID=2556;
ADD GTRX:TRXID=1, TRXNAME="M_RAK_JeerExch_G_1879_18791_A-1", FREQ=24, TRXNO=1, CELLID=639, IDTYPE=BYID, ISMAINBCCH=NO, ISTMPTRX=NO, GTRXGROUPID=2556;
ADD GTRX:TRXID=5, TRXNAME="M_RAK_JeerExch_G_1879_18791_A-2", FREQ=28, TRXNO=2, CELLID=639, IDTYPE=BYID, ISMAINBCCH=NO, ISTMPTRX=NO, GTRXGROUPID=2556;
ADD GTRX:TRXID=6, TRXNAME="M_RAK_JeerExch_G_1879_18791_A-3", FREQ=67, TRXNO=3, CELLID=639, IDTYPE=BYID, ISMAINBCCH=NO, ISTMPTRX=NO, GTRXGROUPID=2556;
更新
最后我按照下面的代码做了它
我又创建了一个 属性 DEFINED_TCH_FRQ = null
用于连接 string.But 问题是它非常慢。我正在迭代文本文件两次,第一次是 sr.readline() 和第二个是通过 File.Readline
获取连接的字符串(这也是我以前使用 File.Readalllines
并出现内存不足异常)
List<int> intarr = new List<int>();
intarr.Clear();
var gtrx = new Gtrx
{
CellId = int.Parse(PullValue(s, "CELLID")),
Freq = int.Parse(PullValue(s, "FREQ")),
TrxNo = int.Parse(PullValue(s, "TRXNO")),
IsMainBcch = PullValue(s, "ISMAINBCCH").ToUpper() == "YES",
Commabcch = new List<string> { PullValue(s, "ISMAINBCCH") },
DEFINED_TCH_FRQ = null,
TrxName = PullValue(s, "TRXNAME"),
};
if (!intarr.Contains(gtrx.CellId))
{
if (!_dictionary.ContainsKey(gtrx.CellId))
{
// No GCell record for this id. Do something!
continue;
}
intarr.Add(gtrx.CellId);
string results = string.Empty;
var result = String.Join(",",
from ss in File.ReadLines(filename)
where ss.Contains("ADD GTRX:")
where int.Parse(PullValue(ss, "CELLID")) == gtrx.CellId
where PullValue(ss, "ISMAINBCCH").ToUpper() != "YES"
select int.Parse(PullValue(ss, "FREQ")));
results = result;
var gtrxnew = new Gtrx
{
DEFINED_TCH_FRQ = results
};
_dictionary[gtrx.CellId].Gtrx = gtrx;
更新
最后我像第一次那样做了,我使用 File.Readalllines 将以 ADD GTRX 开头的行保存到一个数组中,然后仅使用该数组来获取连接的字符串而不是存储整个文本文件并获得了一些性能 improvement.Now 我的问题是,如果我将每个包含数十万行的文本文件转换为 xml,然后从 xml 文件中检索数据,它会提高性能吗?如果我在这里使用数据表和数据集而不是 类,它会提高性能吗?
你没有具体说明到底出了什么问题,但我猜你遇到的问题是由分裂引起的:
string[] str1 = str.Split('=');
这种拆分会导致您的字符串为“CELLID”和“13”(来自您的文件示例)。注意 "CELLID" 前面的 space。这会导致以下代码永远不会通过:
if (str1[0] == "CELLID")
您可以将其更改为:
if (str1[0].Trim() == "CELLID")
可能有用。
假设数据是一致的并且我还假设 GCells 将出现在 GTrx 行之前(因为 GTrx 正在引用 GCell 的 ID),那么您可以创建一个简单的解析器来执行此操作并将值存储在字典。
首先要做的是创建一个 class 来保存 Gtrx 数据和 GCell 数据。请记住,我只是在抓取数据的一个子集。如果您需要更多字段,可以添加到此:
private class Gtrx
{
public int Freq { get; set; }
public int TrxNo { get; set; }
public string TrxName { get; set; }
public int CellId { get; set; }
public bool IsMainBcch { get; set; }
}
private class Gcell
{
public int CellId { get; set; }
public string CellName { get; set; }
public string Mcc { get; set; }
public int Lac { get; set; }
public int Ci { get; set; }
}
除了这些classes,我们还需要一个class到"link"这两个classes在一起:
private class GcellGtrx
{
public Gcell Gcell { get; set; }
public Gtrx Gtrx { get; set; }
}
现在我们可以构建一个简单的解析器了:
private readonly Dictionary<int, GcellGtrx> _dictionary = new Dictionary<int, GcellGtrx>();
string data = "ADD GCELL:CELLID=13, CELLNAME=\"NR_0702_07021_G1_A\", MCC=\"424\", MNC=\"02\", LAC=6112, CI=7021, NCC=6, BCC=0, EXTTP=Normal_cell, IUOTP=Concentric_cell, ENIUO=ON, DBFREQBCCHIUO=Extra, FLEXMAIO=OFF, CSVSP=3, CSDSP=5, PSHPSP=4, PSLPSVP=6, BSPBCCHBLKS=1, BSPAGBLKSRES=4, BSPRACHBLKS=1, TYPE=GSM900_DCS1800, OPNAME=\"Tester\", VIPCELL=NO" + Environment.NewLine;
data = data + "ADD GTRX:TRXID=11140, TRXNAME=\"T_RAK_JaziratHamra_G_702_7021_A-0\", FREQ=99, TRXNO=0, CELLID=13, IDTYPE=BYID, ISMAINBCCH=YES, ISTMPTRX=NO, GTRXGROUPID=80;" + Environment.NewLine;
using (var sr = new StringReader(data))
{
string line = sr.ReadLine();
while (line != null)
{
line = line.Trim();
if (line.StartsWith("ADD GCELL:"))
{
var gcell = new Gcell
{
CellId = int.Parse(PullValue(line, "CELLID")),
CellName = PullValue(line, "CELLNAME"),
Ci = int.Parse(PullValue(line, "CI")),
Lac = int.Parse(PullValue(line, "LAC")),
Mcc = PullValue(line, "MCC")
};
var gcellGtrx = new GcellGtrx();
gcellGtrx.Gcell = gcell;
_dictionary.Add(gcell.CellId, gcellGtrx);
}
if (line.StartsWith("ADD GTRX:"))
{
var gtrx = new Gtrx
{
CellId = int.Parse(PullValue(line, "CELLID")),
Freq = int.Parse(PullValue(line, "FREQ")),
TrxNo = int.Parse(PullValue(line, "TRXNO")),
IsMainBcch = PullValue(line, "ISMAINBCCH").ToUpper() == "YES",
TrxName = PullValue(line, "TRXNAME")
};
if (!_dictionary.ContainsKey(gtrx.CellId))
{
// No GCell record for this id. Do something!
continue;
}
_dictionary[gtrx.CellId].Gtrx = gtrx;
}
line = sr.ReadLine();
}
}
// Now you can pull your data using a CellId:
// GcellGtrx cell13 = _dictionary[13];
//
// Or you could iterate through each one:
// foreach (KeyValuePair<int, GcellGtrx> kvp in _dictionary)
// {
// int key = kvp.Key;
// GcellGtrx gCellGtrxdata = kvp.Value;
// // Do Stuff
// }
最后,我们需要定义一个简单的辅助方法:
private string PullValue(string line, string key)
{
key = key + "=";
int ndx = line.IndexOf(key, 0, StringComparison.InvariantCultureIgnoreCase);
if (ndx >= 0)
{
int ndx2 = line.IndexOf(",", ndx, StringComparison.InvariantCultureIgnoreCase);
if (ndx2 == -1)
ndx2 = line.Length - 1;
return line.Substring(ndx + key.Length, ndx2 - ndx - key.Length).Trim('"').Trim();
}
return "";
}
应该可以!看看这是否不适合你。请记住,这是非常基本的。您可能想要处理一些可能的错误(例如密钥不存在等)。
我有一个包含大量数据的纺织品,首先我必须过滤散落在那里的叶细胞数据 here.For 我过滤的第一行是以 ADD GCELL
开头的行其中包含主要数据,接下来我要做的是我必须通过使用 CELLID
从相同的文本文件中获取相关数据 ADD GCELL
line.Related datas are coming in the以 ADD GTRX
开头的行和数据是 FREQ , TRXNO , ISMAINBCCH ,
。简而言之,CELLID
是行 ADD GCELL
和 ADD GTRX
的共同值。我在 c# 中完成了一些编码,但我被困在某个地方
这是文本文件的一部分
...................................
.........
ADD GCELL:CELLID=13, CELLNAME="NR_0702_07021_G1_A", MCC="424", MNC="02", LAC=6112, CI=7021, NCC=6, BCC=0, EXTTP=Normal_cell, IUOTP=Concentric_cell, ENIUO=ON, DBFREQBCCHIUO=Extra, FLEXMAIO=OFF, CSVSP=3, CSDSP=5, PSHPSP=4, PSLPSVP=6, BSPBCCHBLKS=1, BSPAGBLKSRES=4, BSPRACHBLKS=1, TYPE=GSM900_DCS1800, OPNAME="Tester", VIPCELL=NO
..............................
ADD GTRX:TRXID=11140, TRXNAME="T_RAK_JaziratHamra_G_702_7021_A-0", FREQ=99, TRXNO=0, CELLID=13, IDTYPE=BYID, ISMAINBCCH=YES, ISTMPTRX=NO, GTRXGROUPID=80;
我做的代码是
using (StreamReader sr = File.OpenText(filename))
{
while ((s = sr.ReadLine()) != null)
{
if (s.Contains("ADD GCELL:"))
{
s = s.Replace("ADD GCELL:", "");
string[] items = s.Split(',');
foreach (string str in items)
{
string[] str1 = str.Split('=');
if (str1[0] == "CELLID")
{
cellidnew = str1[1];
}
string fieldname = str1[0];
string value = str1[1].Replace(";", string.Empty).Replace("\"", string.Empty);
}
Getgtrxvalues(filename, ref cellname, ref cellidnew, ref Frequency, ref TRXNO ,ref ISMAINBCCH);
}
}
}
private static void Getgtrxvalues(string filename, ref string cellname, ref string cellid, ref int Frequency, ref int TRXNO ,ref bool ISMAINBCCH)
{
using (StreamReader sr = File.OpenText(filename))
{
while ((s = sr.ReadLine()) != null)
{
if (s.Contains("ADD GTRX:"))
{
try
{
}
}
}
}
更新
一切正常,除了我必须 satisfy.Here 添加 Gtrx 的另一个条件:当 ISMAINBCCH=YES 时,我采用包括 Freq 在内的所有值,但同时 ISMAINBCCH=NO 有 Freq 的值我必须得到逗号分隔的 values.For 示例 就像这里 首先我将采用 FREQ,其中 CELLID = 639(动态的任何事情都可能发生)和 ISMAINBCCH=YES,我现在完成的下一个任务是我必须连接 FREQ以逗号分隔的值,其中 CELLID=639 和 ISMAINBCCH=NO,所以这里我想要的输出是 24,28,67。如何实现这个
行是
ADD GTRX:TRXID=0, TRXNAME="M_RAK_JeerExch_G_1879_18791_A-0", FREQ=81, TRXNO=0, CELLID=639, IDTYPE=BYID, ISMAINBCCH=YES, ISTMPTRX=NO, GTRXGROUPID=2556;
ADD GTRX:TRXID=1, TRXNAME="M_RAK_JeerExch_G_1879_18791_A-1", FREQ=24, TRXNO=1, CELLID=639, IDTYPE=BYID, ISMAINBCCH=NO, ISTMPTRX=NO, GTRXGROUPID=2556;
ADD GTRX:TRXID=5, TRXNAME="M_RAK_JeerExch_G_1879_18791_A-2", FREQ=28, TRXNO=2, CELLID=639, IDTYPE=BYID, ISMAINBCCH=NO, ISTMPTRX=NO, GTRXGROUPID=2556;
ADD GTRX:TRXID=6, TRXNAME="M_RAK_JeerExch_G_1879_18791_A-3", FREQ=67, TRXNO=3, CELLID=639, IDTYPE=BYID, ISMAINBCCH=NO, ISTMPTRX=NO, GTRXGROUPID=2556;
更新
最后我按照下面的代码做了它
我又创建了一个 属性 DEFINED_TCH_FRQ = null
用于连接 string.But 问题是它非常慢。我正在迭代文本文件两次,第一次是 sr.readline() 和第二个是通过 File.Readline
获取连接的字符串(这也是我以前使用 File.Readalllines
并出现内存不足异常)
List<int> intarr = new List<int>();
intarr.Clear();
var gtrx = new Gtrx
{
CellId = int.Parse(PullValue(s, "CELLID")),
Freq = int.Parse(PullValue(s, "FREQ")),
TrxNo = int.Parse(PullValue(s, "TRXNO")),
IsMainBcch = PullValue(s, "ISMAINBCCH").ToUpper() == "YES",
Commabcch = new List<string> { PullValue(s, "ISMAINBCCH") },
DEFINED_TCH_FRQ = null,
TrxName = PullValue(s, "TRXNAME"),
};
if (!intarr.Contains(gtrx.CellId))
{
if (!_dictionary.ContainsKey(gtrx.CellId))
{
// No GCell record for this id. Do something!
continue;
}
intarr.Add(gtrx.CellId);
string results = string.Empty;
var result = String.Join(",",
from ss in File.ReadLines(filename)
where ss.Contains("ADD GTRX:")
where int.Parse(PullValue(ss, "CELLID")) == gtrx.CellId
where PullValue(ss, "ISMAINBCCH").ToUpper() != "YES"
select int.Parse(PullValue(ss, "FREQ")));
results = result;
var gtrxnew = new Gtrx
{
DEFINED_TCH_FRQ = results
};
_dictionary[gtrx.CellId].Gtrx = gtrx;
更新
最后我像第一次那样做了,我使用 File.Readalllines 将以 ADD GTRX 开头的行保存到一个数组中,然后仅使用该数组来获取连接的字符串而不是存储整个文本文件并获得了一些性能 improvement.Now 我的问题是,如果我将每个包含数十万行的文本文件转换为 xml,然后从 xml 文件中检索数据,它会提高性能吗?如果我在这里使用数据表和数据集而不是 类,它会提高性能吗?
你没有具体说明到底出了什么问题,但我猜你遇到的问题是由分裂引起的:
string[] str1 = str.Split('=');
这种拆分会导致您的字符串为“CELLID”和“13”(来自您的文件示例)。注意 "CELLID" 前面的 space。这会导致以下代码永远不会通过:
if (str1[0] == "CELLID")
您可以将其更改为:
if (str1[0].Trim() == "CELLID")
可能有用。
假设数据是一致的并且我还假设 GCells 将出现在 GTrx 行之前(因为 GTrx 正在引用 GCell 的 ID),那么您可以创建一个简单的解析器来执行此操作并将值存储在字典。
首先要做的是创建一个 class 来保存 Gtrx 数据和 GCell 数据。请记住,我只是在抓取数据的一个子集。如果您需要更多字段,可以添加到此:
private class Gtrx
{
public int Freq { get; set; }
public int TrxNo { get; set; }
public string TrxName { get; set; }
public int CellId { get; set; }
public bool IsMainBcch { get; set; }
}
private class Gcell
{
public int CellId { get; set; }
public string CellName { get; set; }
public string Mcc { get; set; }
public int Lac { get; set; }
public int Ci { get; set; }
}
除了这些classes,我们还需要一个class到"link"这两个classes在一起:
private class GcellGtrx
{
public Gcell Gcell { get; set; }
public Gtrx Gtrx { get; set; }
}
现在我们可以构建一个简单的解析器了:
private readonly Dictionary<int, GcellGtrx> _dictionary = new Dictionary<int, GcellGtrx>();
string data = "ADD GCELL:CELLID=13, CELLNAME=\"NR_0702_07021_G1_A\", MCC=\"424\", MNC=\"02\", LAC=6112, CI=7021, NCC=6, BCC=0, EXTTP=Normal_cell, IUOTP=Concentric_cell, ENIUO=ON, DBFREQBCCHIUO=Extra, FLEXMAIO=OFF, CSVSP=3, CSDSP=5, PSHPSP=4, PSLPSVP=6, BSPBCCHBLKS=1, BSPAGBLKSRES=4, BSPRACHBLKS=1, TYPE=GSM900_DCS1800, OPNAME=\"Tester\", VIPCELL=NO" + Environment.NewLine;
data = data + "ADD GTRX:TRXID=11140, TRXNAME=\"T_RAK_JaziratHamra_G_702_7021_A-0\", FREQ=99, TRXNO=0, CELLID=13, IDTYPE=BYID, ISMAINBCCH=YES, ISTMPTRX=NO, GTRXGROUPID=80;" + Environment.NewLine;
using (var sr = new StringReader(data))
{
string line = sr.ReadLine();
while (line != null)
{
line = line.Trim();
if (line.StartsWith("ADD GCELL:"))
{
var gcell = new Gcell
{
CellId = int.Parse(PullValue(line, "CELLID")),
CellName = PullValue(line, "CELLNAME"),
Ci = int.Parse(PullValue(line, "CI")),
Lac = int.Parse(PullValue(line, "LAC")),
Mcc = PullValue(line, "MCC")
};
var gcellGtrx = new GcellGtrx();
gcellGtrx.Gcell = gcell;
_dictionary.Add(gcell.CellId, gcellGtrx);
}
if (line.StartsWith("ADD GTRX:"))
{
var gtrx = new Gtrx
{
CellId = int.Parse(PullValue(line, "CELLID")),
Freq = int.Parse(PullValue(line, "FREQ")),
TrxNo = int.Parse(PullValue(line, "TRXNO")),
IsMainBcch = PullValue(line, "ISMAINBCCH").ToUpper() == "YES",
TrxName = PullValue(line, "TRXNAME")
};
if (!_dictionary.ContainsKey(gtrx.CellId))
{
// No GCell record for this id. Do something!
continue;
}
_dictionary[gtrx.CellId].Gtrx = gtrx;
}
line = sr.ReadLine();
}
}
// Now you can pull your data using a CellId:
// GcellGtrx cell13 = _dictionary[13];
//
// Or you could iterate through each one:
// foreach (KeyValuePair<int, GcellGtrx> kvp in _dictionary)
// {
// int key = kvp.Key;
// GcellGtrx gCellGtrxdata = kvp.Value;
// // Do Stuff
// }
最后,我们需要定义一个简单的辅助方法:
private string PullValue(string line, string key)
{
key = key + "=";
int ndx = line.IndexOf(key, 0, StringComparison.InvariantCultureIgnoreCase);
if (ndx >= 0)
{
int ndx2 = line.IndexOf(",", ndx, StringComparison.InvariantCultureIgnoreCase);
if (ndx2 == -1)
ndx2 = line.Length - 1;
return line.Substring(ndx + key.Length, ndx2 - ndx - key.Length).Trim('"').Trim();
}
return "";
}
应该可以!看看这是否不适合你。请记住,这是非常基本的。您可能想要处理一些可能的错误(例如密钥不存在等)。