• 更改文本文件分隔符


    【问题】

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

     

  • 相关阅读:
    ImportError: cannot import name ‘xxx‘ from ‘xxx‘关于python导包的问题
    酒店预订订房小程序源码系统+多酒店入驻 功能齐全 附带完整的搭建教程
    当“黑客”遇到电脑初学者.....看完笑死我了
    初识TypeScript编译器(tsc)
    Docker的容器管理操作
    centos Hadoop伪分布模式安装-ssh免密登录
    HBase非关系型数据库
    【机器学习】实验3布置:贝叶斯垃圾邮件识别
    C#中File类常见用法总结
    EL表达式与JSTL标签
  • 原文地址:https://blog.csdn.net/raqsoft/article/details/126008205