我想制作一个简单的代码,计算txt文件中前三个最常见的行/文本,然后将该行/文本保存到另一个文本文件中(这将反过来读入AutoCAD的变量系统)。
忘记AutoCAD部分,我可以管理我如何在VB.net中将3个最常重复的文本行保存到自己的文本文件中,请参阅下面的示例:
要读取的文本文件如下:
APG BTR VTS VTS VTS VTS BTR BTR APG PNG
VB.net程序然后将文本VTS保存到mostused.txt BTR到2ndmostused.txt和APG到3rdmostused.txt
如何才能最好地实现这一目标?
答案 0 :(得分:0)
由于我是C#开发人员,我将使用它:
var dict = new Dictionary<string, int>();
using(var sr = new StreamReader(file))
{
var line = string.Empty;
while ((line = sr.ReadLine()) != null)
{
var words = line.Split(' '); // get the words
foreach(var word in words)
{
if(!dict.Contains(word)) dict.Add(word, 0);
dict[word]++; // count them
}
}
}
var query = from d in dict select d order by d.Value; // now you have it sorted
int counter = 1;
foreach(var pair in query)
{
using(var sw = new StreamWriter("file" + counter + ".txt"))
sw.writer(pair.Key);
}