首先:我必须按省份从csv(https://pastebin.com/jxNSzVYP)中进行统计。在某个地方,您可以找到像“ HU-GD”这样的省,然后您必须看到“ HU”这样的前2个字母。而且我必须计算那里有多少个SI / GA /等省。之后,我必须写出至少为3的值。(如果csv更改,我的意思是将不再有HU / SI /等,并且将会有新的HU / SI /等,程序必须对此进行计数。)[检查:描述预期结果]
第二:我必须制作一个新的“ newCNtunnels.csv”文件。当有这样的省份“ HU-GD”时,我必须将它们分开并用相同的数据写入它们,只是该省份有所不同。 [检查:描述预期结果]
我不是一个大型程序员,所以请不要使这个复杂化。 对不起我的英语不好。这不是我的母语,但您可能已经注意到了。 感谢您的帮助!
我正在使用Windows 10和Visual Studio。 C#语言
struct datas
{
public string name;
public int length;
public string date;
public string province;
}
datas[] tunnel = new datas[99];
int i = 0;
int howmanyrow= 0;
StreamReader sr = new StreamReader("CNtunnels.csv");
sr.ReadLine();
while (!sr.EndOfStream)
{
String[] onerow= sr.ReadLine().Split(';');
tunnel[i].name= onerow[0];
tunnel[i].length= Convert.ToInt32(onerow[1]);
tunnel[i].date= onerow[2];
tunnel[i].province= onerow[3];
i++;
howmanyrow++;
}
sr.Close();
例外结果:
答案 0 :(得分:0)
请参阅以下解决方案:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
const string INPUT_CSV_FILENAME = @"c:\temp\test.csv";
const string OUTPUT_CSV_FILENAME = @"c:\temp\test1.csv";
const string OUTPUT_STATISTICS_FILENAME = @"c:\temp\statistic.txt";
static void Main(string[] args)
{
Data data = new Data(INPUT_CSV_FILENAME);
data.WriteStatistics(OUTPUT_STATISTICS_FILENAME);
data.WriteCSV(OUTPUT_CSV_FILENAME);
}
}
class Data
{
public static List<Data> datas = new List<Data>();
public string name;
public int length;
public string date;
public string province;
public Data() { }
public Data(string filename)
{
StreamReader reader = new StreamReader(filename);
string line = "";
int rowCount = 0;
while ((line = reader.ReadLine()) != null)
{
line = line.Trim();
if (line.Length > 0)
{
string[] splitArray = line.Split(new char[] { ';' });
if (++rowCount > 1)
{
string[] splitProvidence = splitArray[3].Split(new char[] { '-' });
foreach (string providence in splitProvidence)
{
Data newRow = new Data();
Data.datas.Add(newRow);
newRow.name = splitArray[0];
newRow.length = int.Parse(splitArray[1]);
newRow.date = splitArray[2];
newRow.province = providence;
}
}
}
}
}
public void WriteStatistics(string filename)
{
StreamWriter writer = new StreamWriter(filename);
var groups = datas.GroupBy(x => x.province).OrderByDescending(x => x.Count()).ToList();
foreach (var group in groups)
{
writer.WriteLine("{0} - {1}", group.Key, group.Count());
}
writer.Flush();
writer.Close();
}
public void WriteCSV(string filename)
{
StreamWriter writer = new StreamWriter(filename);
string header = string.Join(";", new string[] { "name","length","date","province"});
writer.WriteLine(header);
foreach (Data data in datas)
{
writer.WriteLine(string.Join(";",new string[] {
data.name,
data.length.ToString(),
data.date,
data.province
}));
}
writer.Flush();
writer.Close();
}
}
}