[英]How to get data's from text file in c#
我有一個包含大量數據的紡織品,第一件事是我必須過濾散布在那里和此處的葉子細胞數據。對於這第一行,我過濾的是以ADD GCELL
開頭的行,其中包含主要數據,接下來我要做的是我以獲得來自同一個文本文件中的相關數據CELLID
在同一個未來ADD GCELL
line.Related DATAS該行是來與beggining 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
{
}
}
}
}
更新
一切正常,除了我必須滿足的另外一個條件。這里是ADD Gtrx:當ISMAINBCCH = YES時,我將使用包括Freq在內的所有值,但同時ISMAINBCCH = NO時,我必須將逗號獲取的Freq值例如,首先我將使用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
以獲取串聯的字符串。但是問題是它非常慢。我迭代文本文件兩次,第一次是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開頭的行保存到數組中,然后僅使用該數組來獲取串聯的字符串,而不是存儲整個文本文件並獲得了一些性能提升。現在我的問題是我是否將每個包含數十萬行的文本文件轉換為xml,然后從xml文件中檢索數據,這會提高性能嗎? 如果我在這里使用數據表和數據集而不是類,是否會改善性能?
假設數據是一致的,並且我還假設GCell將在GTrx行之前(因為GTrx引用了GCell的ID),那么您可以創建一個簡單的解析器來將其存儲在字典中。
首先要做的是創建一個類來保存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; }
}
除了這些類之外,我們還將需要一個類來將這兩個類“鏈接”在一起:
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 "";
}
那應該做! 看看那對您不起作用。 請記住,這是非常基本的。 您可能希望處理一些可能的錯誤(例如,鍵不存在等)。
您沒有指定到底出了什么問題,但是我想您遇到的問題是由拆分引起的:
string[] str1 = str.Split('=');
這種拆分會使您的字符串分別為“ CELLID”和“ 13”(來自您的文件示例)。 注意“ CELLID”前面的空間。 這將導致以下代碼永不通過:
if (str1[0] == "CELLID")
您可以將其更改為:
if (str1[0].Trim() == "CELLID")
它可能會起作用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.