码迷,mamicode.com
首页 > 其他好文 > 详细

Split CSV/TXT file

时间:2018-06-09 19:47:21      阅读:182      评论:0      收藏:0      [点我收藏+]

标签:ast   inpu   ++   pat   XA   file   git   dde   nts   

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

标签:ast   inpu   ++   pat   XA   file   git   dde   nts   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!