• 更改文本文件分隔符


    【问题】

    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!!

    题主自己写的待改进代码

    1. public override void Input0_ProcessInputRow(Input0Buffer Row)
    2.     {
    3.         /*
    4.          * Add your code here
    5.          */
    6.         String inputString = Row.line.ToString();
    7.         int count = Row.line.Split('"').Length - 1;
    8.         int counter = 1;
    9.         while (counter < count)
    10.         {
    11.             if (counter % 2 == 0)
    12.             {
    13.                 int StartIndex = GetNthIndex(inputString.ToString(), Convert.ToChar("\""), counter);
    14.                 int EndIndex = GetNthIndex(inputString.ToString(), Convert.ToChar("\""), counter + 1);
    15.                 inputString = inputString.ToString().Substring(0, StartIndex + 1) + "," +
    16.                 inputString.ToString().Substring(EndIndex);
    17.             }
    18.             else
    19.             {
    20.             }
    21.             counter = counter + 1;
    22.         }
    23.         Row.OutputLine = inputString;
    24.     }
    25.     int GetNthIndex(string s, char t, int n)
    26.     {
    27.         int count = 0;
    28.         for (int i = 0; i < s.Length; i++)
    29.         {
    30.             if (s[i] == t)
    31.             {
    32.                 count++;
    33.                 if (count == n)
    34.                 {
    35.                     return i;
    36.                 }
    37.             }
    38.         }
    39.         return -1;
    40.     }

    【回答】

    更改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 脚本

     

  • 相关阅读:
    Python 读写 Excel 文件
    如何给视频加上酷炫边框?简单几步搞定
    Java IO流(详解)
    ARM 和 AMD 架构的区别
    Kubernetes kubectl 全组件异常诊断方法(全干货)
    监控指标--监控指标(3)
    ssh免密登录远程服务器
    移动互联网的下半场,产品经理们该变了
    【AnolisOS 8.x】配置本地 repo 源
    # Python基础:输入输出详解-读写文件(还需完善)
  • 原文地址:https://blog.csdn.net/raqsoft/article/details/126008205