更改文本文件分隔符
【问题】
I am trying to import a text file in which tab delimited and text qualifier is double quotes.
I want following text
"655295" "Keisuke" "" "Ueda" "1-2-2, Central Park East F201" "Utase, Mihama-Ku"
to convert to
"655295","Keisuke","","Ueda","1-2-2, Central Park East F201","Utase, Mihama-Ku"
I tried derived column transformation, but it did not help. I tried script component, but that didn't work either. Can someone please help me out here.
Thank you in advance!!
题主自己写的待改进代码
public override void Input0_ProcessInputRow(Input0Buffer Row)
{
/*
* Add your code here
*/
String inputString = Row.line.ToString();
int count = Row.line.Split('"').Length - 1;
int counter = 1;
while (counter < count)
{
if (counter % 2 == 0)
{
int StartIndex = GetNthIndex(inputString.ToString(), Convert.ToChar("\""), counter);
int EndIndex = GetNthIndex(inputString.ToString(), Convert.ToChar("\""), counter + 1);
inputString = inputString.ToString().Substring(0, StartIndex + 1) + "," +
inputString.ToString().Substring(EndIndex);
}
else
{
}
counter = counter + 1;
}
Row.OutputLine = inputString;
}
int GetNthIndex(string s, char t, int n)
{
int count = 0;
for (int i = 0; i < s.Length; i++)
{
if (s[i] == t)
{
count++;
if (count == n)
{
return i;
}
}
}
return -1;
}
【回答】
更改csv文件的分隔符,这种工作可以在外部做,比如用SPL来实现,脚本如下:
A |
|
1 |
=file("d:\\source.txt ").import() |
2 |
=file("d:\\target.txt").export(A1;",") |
A1:读取文本source.txt
A2:将 A1 中的分隔符改成逗号后导出到target.txt文本中
写好的脚本如何在应用程序中调用,可以参考Java 如何调用 SPL 脚本