如何在用作变量的文件名中加入文本限定符?
How to incorporate a text qualifier in a file name used as a variable?
我有一个 C# 脚本,它将两个 CSV 文件作为输入,合并这两个文件,对它们执行大量计算,并将结果写入一个新的 CSV 文件。
这两个输入 CSV 文件名被声明为变量,并通过访问这些变量名在 C# 脚本中使用。
输入 CSV 文件中的数据如下所示:
由于数据的值以千和百万为单位,C# 代码中的行拆分错误地截断了数据。例如,值 11,861 仅显示为 11 和 681 进入下一列。
在 C# 中有什么方法可以为两个文件指定文本限定符(在本例中为 ")?
这是 C# 代码片段:
string[,] filesToProcess = new string[2, 2] { {(String)Dts.Variables["csvFileNameUSD"].Value,"USD" }, {(String)Dts.Variables["csvFileNameCAD"].Value,"CAD" } };
string headline = "CustType,CategoryType,CategoryValue,DataType,Stock QTY,Stock Value,Floor QTY,Floor Value,Order Count,Currency";
string outPutFile = Dts.Variables["outputFile"].Value.ToString();
//Declare Output files to write to
FileStream sw = new System.IO.FileStream(outPutFile, System.IO.FileMode.Create);
StreamWriter w = new StreamWriter(sw);
w.WriteLine(headline);
//Loop Through the files one by one and write to output Files
for (int x = 0; x < filesToProcess.GetLength(1); x++)
{
if (System.IO.File.Exists(filesToProcess[x, 0]))
{
string categoryType = "";
string custType = "";
string dataType = "";
string categoryValue = "";
//Read the input file in memory and close after done
StreamReader sr = new StreamReader(filesToProcess[x, 0]);
string fileText = sr.ReadToEnd();
string[] lines = fileText.Split(Convert.ToString(System.Environment.NewLine).ToCharArray());
sr.Close();
其中 csvFileNameUSD 和 csvFileNameCAD 是变量,其值指向它们的位置。
您几天前发布了一张非常相似的照片 。该解决方案对您没有帮助吗?
如果是这样,您在这方面遇到了什么问题。我们或许也可以帮助您解决该问题。
嗯,根据你回答的问题,这应该是你想做的:
public void SomeMethodInYourCodeSnippet()
{
string[] lines;
using (StreamReader sr = new StreamReader(filesToProcess[x, 0]))
{
//Read the input file in memory and close after done
string fileText = sr.ReadToEnd();
lines = fileText.Split(Convert.ToString(System.Environment.NewLine).ToCharArray());
sr.Close(); // redundant due to using, but just to be safe...
}
foreach (var line in lines)
{
string[] columnValues = GetColumnValuesFromLine(line);
// Do whatever with your column values here...
}
}
private string[] GetColumnValuesFromLine(string line)
{
// Split on ","
var values = line.Split(new string [] {"\",\""}, StringSplitOptions.None);
if (values.Count() > 0)
{
// Trim leading double quote from first value
var firstValue = values[0];
if (firstValue.Length > 0)
values[0] = firstValue.Substring(1);
// Trim the trailing double quote from the last value
var lastValue = values[values.Length - 1];
if (lastValue.Length > 0)
values[values.Length - 1] = lastValue.Substring(0, lastValue.Length - 1);
}
return values;
}
试一试,让我知道它是如何工作的!
我有一个 C# 脚本,它将两个 CSV 文件作为输入,合并这两个文件,对它们执行大量计算,并将结果写入一个新的 CSV 文件。 这两个输入 CSV 文件名被声明为变量,并通过访问这些变量名在 C# 脚本中使用。
输入 CSV 文件中的数据如下所示:
由于数据的值以千和百万为单位,C# 代码中的行拆分错误地截断了数据。例如,值 11,861 仅显示为 11 和 681 进入下一列。
在 C# 中有什么方法可以为两个文件指定文本限定符(在本例中为 ")?
这是 C# 代码片段:
string[,] filesToProcess = new string[2, 2] { {(String)Dts.Variables["csvFileNameUSD"].Value,"USD" }, {(String)Dts.Variables["csvFileNameCAD"].Value,"CAD" } };
string headline = "CustType,CategoryType,CategoryValue,DataType,Stock QTY,Stock Value,Floor QTY,Floor Value,Order Count,Currency";
string outPutFile = Dts.Variables["outputFile"].Value.ToString();
//Declare Output files to write to
FileStream sw = new System.IO.FileStream(outPutFile, System.IO.FileMode.Create);
StreamWriter w = new StreamWriter(sw);
w.WriteLine(headline);
//Loop Through the files one by one and write to output Files
for (int x = 0; x < filesToProcess.GetLength(1); x++)
{
if (System.IO.File.Exists(filesToProcess[x, 0]))
{
string categoryType = "";
string custType = "";
string dataType = "";
string categoryValue = "";
//Read the input file in memory and close after done
StreamReader sr = new StreamReader(filesToProcess[x, 0]);
string fileText = sr.ReadToEnd();
string[] lines = fileText.Split(Convert.ToString(System.Environment.NewLine).ToCharArray());
sr.Close();
其中 csvFileNameUSD 和 csvFileNameCAD 是变量,其值指向它们的位置。
您几天前发布了一张非常相似的照片
如果是这样,您在这方面遇到了什么问题。我们或许也可以帮助您解决该问题。
嗯,根据你回答的问题,这应该是你想做的:
public void SomeMethodInYourCodeSnippet()
{
string[] lines;
using (StreamReader sr = new StreamReader(filesToProcess[x, 0]))
{
//Read the input file in memory and close after done
string fileText = sr.ReadToEnd();
lines = fileText.Split(Convert.ToString(System.Environment.NewLine).ToCharArray());
sr.Close(); // redundant due to using, but just to be safe...
}
foreach (var line in lines)
{
string[] columnValues = GetColumnValuesFromLine(line);
// Do whatever with your column values here...
}
}
private string[] GetColumnValuesFromLine(string line)
{
// Split on ","
var values = line.Split(new string [] {"\",\""}, StringSplitOptions.None);
if (values.Count() > 0)
{
// Trim leading double quote from first value
var firstValue = values[0];
if (firstValue.Length > 0)
values[0] = firstValue.Substring(1);
// Trim the trailing double quote from the last value
var lastValue = values[values.Length - 1];
if (lastValue.Length > 0)
values[values.Length - 1] = lastValue.Substring(0, lastValue.Length - 1);
}
return values;
}
试一试,让我知道它是如何工作的!