0

フラットファイルからの列を数える方法を見つけようとしています。実際、すべての列は「|」で区切られた記号セルに連結されています。、
さまざまな試みの後、これを処理できるのはスクリプト タスクだけのようです。誰でも私を助けることができますか?私は恥ずべきことに、C# や VB でスクリプトを使用した経験がありません。

どうもありがとうエマニュエル

理解を深めるために、以下は私が達成したいことの出力です。たとえば、FF からのすべてのヘッダーを含む単一のセル。問題は、この結果を得るために、前のステップ (派生列) ですべての列名を手動で追加して、それらを '|' で連結することです。セパレーター。今、私の FF ソース レイアウトが変更された場合、この手作業のプロセスのために、それは動作しなくなります。したがって、代わりにスクリプトを使用する必要があると思います。これは、基本的に変数で列数 (ヘッダー) を返し、たとえば、派生列のトランスフォでハードコードされた部分を削除できるようにします。

4

2 に答える 2

1

これは非常に古いスレッドです。ただし、同様の問題に遭遇しました。内部に多数の異なるレコード「フォーマット」を持つフラット ファイル。特定の順序ではなく、さまざまな形式があります。つまり、1 行に 57 フィールド、次の 1000 に 59、次の 10000 に 56、57 に戻るということです。

より良いアイデアがないため、各行のコンマの数に基づいてそのファイルを分割し、各タイプの SSIS パッケージを使用して、さまざまなレコード タイプ (現在はまとめられている) をインポートすることにしました。

したがって、この質問に対する答えはそこにあり、ファイルを生成するためのコードがもう少しあります。

これが同じ問題を抱えている人に役立つことを願っています。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace OddFlatFile_Transformation
{
    class RedistributeLines
    {
    /*
     * This routine opens a text file and reads it line by line
     * for each line the number of "," (commas) is counted
     * and then the line is written into a another text file
     * based on that number of commas found
     * For example if there are 15 commas in a given line
     * the line is written to the WhateverFileName_15.Ext
     * WhaeverFileName and Ext are the same file name and 
     * extension from the original file that is being read
     * The application tests WhateverFileName_NN.Ext for existance
     * and creates the file in case it does not exist yet
     * To Better control splited records a sequential identifier, 
     * based on the number of lines read, is added to the beginning
     * of each line written independently of the file and record number
     */
        static void Main(string[] args)
        {
            // get full qualified file name from console
            String strFileToRead;
            strFileToRead = Console.ReadLine();

            // create reader & open file
            StreamReader srTextFileReader = new StreamReader(strFileToRead);

            string strLineRead = "";
            string strFileToWrite = "";
            string strLineIdentifier = "";
            string strLineToWrite = "";
            int intCountLines = 0;
            int intCountCommas = 0;
            int intDotPosition = 0;
            const string strZeroPadding = "00000000";

            // Processing begins
            Console.WriteLine("Processing begins: " + DateTime.Now);

            /* Main Loop */
            while (strLineRead != null)
            {
                // read a line of text count commas and create Linde Identifier
                strLineRead = srTextFileReader.ReadLine();
                if (strLineRead != null)
                {
                    intCountLines += 1;
                    strLineIdentifier = strZeroPadding.Substring(0, strZeroPadding.Length - intCountLines.ToString().Length) + intCountLines;
                    intCountCommas = 0;
                    foreach (char chrEachPosition in strLineRead)
                    {
                        if (chrEachPosition == ',') intCountCommas++;
                    }

                    // Based on the number of commas determined above
                    // the name of the file to be writen to is established
                    intDotPosition = strFileToRead.IndexOf(".");
                    strFileToWrite = strFileToRead.Substring (0,intDotPosition) + "_";
                    if ( intCountCommas < 10)
                    {
                        strFileToWrite += "0" + intCountCommas;
                    }
                    else
                    {
                        strFileToWrite += intCountCommas;
                    }
                    strFileToWrite += strFileToRead.Substring(intDotPosition, (strFileToRead.Length - intDotPosition));

                    // Using the file name established above the line captured
                    // during the text read phase is written to that file

                    StreamWriter swTextFileWriter = new StreamWriter(strFileToWrite, true);
                    strLineToWrite = "[" + strLineIdentifier + "] " + strLineRead; 
                    swTextFileWriter.WriteLine (strLineToWrite);
                    swTextFileWriter.Close();
                     Console.WriteLine(strLineIdentifier);
               }
            }

            // close the stream
            srTextFileReader.Close();
            Console.WriteLine(DateTime.Now);
            Console.ReadLine();
        }
    }


}
于 2014-03-30T17:39:41.283 に答える
0

以下の質問で私の回答を参照してくださいStack Overflow。これらの回答から、さまざまな数の列を含むフラット ファイルをロードする方法がわかります。

  1. 次の質問の例では、特殊文字で区切られたデータを含むファイルを読み取りますÇ (c-cedilla)。あなたの場合、区切り文字はVertical Bar (|) UTF-8 フラット ファイルです SQL Server 2008 へのインポートは {LF} 行区切り文字を認識しません

  2. 次の質問の例では、列数が異なるさまざまなセクションを含む EDI ファイルを読み取ります。パッケージはファイルを読み取り、それに応じて親子関係を SQL テーブルにロードします。 ヘッダーと詳細の親子関係を含むフラット ファイルを SQL サーバーにロードする方法

これらの回答で使用されているロジックに基づいて、列区切り文字でファイル内の行を分割することにより、列の数をカウントすることもできます(Vertical Bar |)

それが役立つことを願っています。

于 2011-06-13T11:57:38.627 に答える