C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
C# program that parses CSV string
using System;
class Program
{
static void Main()
{
string text = "field one,field2,description,identity";
// Split the cvs on a comma.
string[] parts = text.Split(',');
foreach (string value in parts)
{
Console.WriteLine(value);
}
}
}
Output
field one
field2
description
identity
Example: Here we see a static class. It divides a large input CSV file, such as example.csv, into smaller files of one megabyte.
Here: Pay attention to the method call in the Main method, which specifies files of 1024 times 1024 bytes, or one megabyte.
Main argsFile: We use File.ReadLines to read in the entire source CSV file. In the for-loop, it adds up the current byte length of the strings.
And: When it exceeds the maximum length in bytes, it outputs a new file. It generates file names "split_00.txt", "split_01.txt" and more.
C# program that segments CSV files
using System;
class Program
{
static void Main()
{
// Split this CSV file into 1 MB chunks.
CSVSplitTool.SplitCSV("example.csv", "split", 1024 * 1024);
}
}
/// <summary>
/// Tool for splitting CSV files at a certain byte size on a line break.
/// </summary>
static class CSVSplitTool
{
/// <summary>
/// Split CSV files on line breaks before a certain size in bytes.
/// </summary>
public static void SplitCSV(string file, string prefix, int size)
{
// Read lines from source file
string[] arr = System.IO.File.ReadAllLines(file);
int total = 0;
int num = 0;
var writer = new System.IO.StreamWriter(GetFileName(prefix, num));
// Loop through all source lines
for (int i = 0; i < arr.Length; i++)
{
// Current line
string line = arr[i];
// Length of current line
int length = line.Length;
// See if adding this line would exceed the size threshold
if (total + length >= size)
{
// Create a new file
num++;
total = 0;
writer.Dispose();
writer = new System.IO.StreamWriter(GetFileName(prefix, num));
}
// Write the line to the current file
writer.WriteLine(line);
// Add length of line in bytes to running size
total += length;
// Add size of newlines
total += Environment.NewLine.Length;
}
writer.Dispose();
}
/// <summary>
/// Get an output file name based on a number.
/// </summary>
static string GetFileName(string prefix, int num)
{
return prefix + "_" + num.ToString("00") + ".txt";
}
}
And: Each field is a random number. The sum of the six output files is 6.11 MB, which is the same as the input file.
Results: The first five output files are 1024 KB each. This is displayed as 0.99 MB in the file manager. The final file is 116 KB.
Also: The lines in the output files were also checked for accuracy. The first file split occurs after line 9816.
Therefore: Line 9816 is the final line in the first output file, and line 9817 is the first line in the second output file.