将 CSV 转换为新格式 (C#)
Transpose CSV to new format (C#)
我有 csv 文件:
我需要将其转换为以下格式:
我只做了这个:
StreamReader srd = new StreamReader(path: path);
int rowCount = 0;
string[] oStreamDataValues = null;
string[] headerNames = null;
while (!srd.EndOfStream)
{
string oStreamRowData = srd.ReadLine().Trim();
oStreamDataValues = oStreamRowData.Split(';');
//headers
if (rowCount == 0)
{
headerNames = oStreamDataValues;
rowCount++;//second row go to "else"
}
//create new csv
else
{
}
//rowCount++;
}
//Console.WriteLine("number of lines " + rowCount);
srd.Dispose();
你能帮我转换成新格式吗?
你可以试试这个 else 子句:
else
{
string leftColumn = oStreamDataValues[0];
for (int i = 1; i < oStreamDataValues.Length; i++)
{
string middleColumn = headerNames[i];
string rightColumn = oStreamDataValues[i];
string newRow = string.Join(";", leftColumn, middleColumn, rightColumn, Environment.NewLine);
File.AppendAllText(pathToNewFile, newRow);
}
}
编辑:或使用 Linq:
var result = headerNames.Skip(1).Zip(oStreamDataValues.Skip(1), (a, b) => String.Join(";", oStreamDataValues[0], a, b));
using System;
using System.IO;
namespace SharpTest
{
class Program
{
static void Main(string[] args)
{
string path = "XXX/test.csv";
string savePath = "XXX/test2.txt";
using(StreamReader sr = new StreamReader(path))
{
string headerLine = sr.ReadLine();
string[] headers = headerLine.Split(';');
using(StreamWriter sw = new StreamWriter(savePath))
{
while (true)
{
string line = sr.ReadLine();
if (line == null) return;
string[] ss = line.Split(';');
if (ss.Length != headers.Length) continue;
for (int i = 1; i < headers.Length; i++)
{
sw.WriteLine(ss[0] + ";" + headers[i] + ";" + ss[i]);
}
}
}
}
}
}
}
测试 CSV:
ID;Col2;Col3;Col4
id1;10;def;20
Id2;30;aaa;40
我有 csv 文件:
我需要将其转换为以下格式:
我只做了这个:
StreamReader srd = new StreamReader(path: path);
int rowCount = 0;
string[] oStreamDataValues = null;
string[] headerNames = null;
while (!srd.EndOfStream)
{
string oStreamRowData = srd.ReadLine().Trim();
oStreamDataValues = oStreamRowData.Split(';');
//headers
if (rowCount == 0)
{
headerNames = oStreamDataValues;
rowCount++;//second row go to "else"
}
//create new csv
else
{
}
//rowCount++;
}
//Console.WriteLine("number of lines " + rowCount);
srd.Dispose();
你能帮我转换成新格式吗?
你可以试试这个 else 子句:
else
{
string leftColumn = oStreamDataValues[0];
for (int i = 1; i < oStreamDataValues.Length; i++)
{
string middleColumn = headerNames[i];
string rightColumn = oStreamDataValues[i];
string newRow = string.Join(";", leftColumn, middleColumn, rightColumn, Environment.NewLine);
File.AppendAllText(pathToNewFile, newRow);
}
}
编辑:或使用 Linq:
var result = headerNames.Skip(1).Zip(oStreamDataValues.Skip(1), (a, b) => String.Join(";", oStreamDataValues[0], a, b));
using System;
using System.IO;
namespace SharpTest
{
class Program
{
static void Main(string[] args)
{
string path = "XXX/test.csv";
string savePath = "XXX/test2.txt";
using(StreamReader sr = new StreamReader(path))
{
string headerLine = sr.ReadLine();
string[] headers = headerLine.Split(';');
using(StreamWriter sw = new StreamWriter(savePath))
{
while (true)
{
string line = sr.ReadLine();
if (line == null) return;
string[] ss = line.Split(';');
if (ss.Length != headers.Length) continue;
for (int i = 1; i < headers.Length; i++)
{
sw.WriteLine(ss[0] + ";" + headers[i] + ";" + ss[i]);
}
}
}
}
}
}
}
测试 CSV:
ID;Col2;Col3;Col4
id1;10;def;20
Id2;30;aaa;40