首页 > 其他 > 详细

Split CSV/TXT file

时间:2018-06-09 19:32:11      阅读:199      评论:0      收藏:0      [点我收藏+]

void Main()
{
var path = @"c:\sourceGit\speciesLatLon.txt";
var inputLines = File.ReadAllLines(path);

// Holds all the lines to be added to each output file
var linesForCurrentSpeciesFile = new List<string>();

// Read first row
int i = 0;
var currentSpecies = GetSpecies(inputLines[i]);

// initialize hold value
var holdValue = currentSpecies;

// Initialize output values
linesForCurrentSpeciesFile.Add(inputLines[i]);

// Read next value
i++;

while( i < inputLines.Length )
{
currentSpecies = GetSpecies(inputLines[i]);
if (currentSpecies != holdValue)
{
// output current file
WriteSpeciesFile(holdValue, linesForCurrentSpeciesFile);

// Initialize new output file by clearing out the previous
linesForCurrentSpeciesFile.Clear();

// update hold value with the value just examined.
holdValue = currentSpecies;
}
// Add the current line to the output file
linesForCurrentSpeciesFile.Add(inputLines[i]);
i++;
}
// Write the output file because last row is equal to a break in the sequence
WriteSpeciesFile(currentSpecies, linesForCurrentSpeciesFile);
}

// Define other methods and classes here
public string GetSpecies(string line)
{
// return the first value of the input line
return line.Split(new char[] {‘,‘})[0];
}

public void WriteSpeciesFile(string species, List<string> content)
{
File.WriteAllLines(string.Format(@"C:\sourceGit\{0}.csv", species), content.ToArray());
}

Split CSV/TXT file

原文:https://www.cnblogs.com/nichoxxc/p/9160534.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!