0

ファイルのダウンロードなど、Webアクティビティをシミュレートするカスタムシステムを開発しました。このシステムにフィードするカスタムファイル形式もあります。私はperlで書かれたこの古いシステムをpythonの新しいシステムに変更しようとしています。しかし、最初に私はどういうわけかファイルを解析する必要があります。

ファイルには、システムに関する議論がある場所など、解析したい特定のフィールドがあります[settings][macro]重要なもの(ステップなど)の始まりであるセクションもあります。

私が問題を抱えているのは、これらのセクションを解析して、システムに別のはるかに単純な形式で書き出すことです(これらのファイルは何千もあり、古いファイルを取得して新しい形式で新しい形式に書き込むジェネレーターを作成したいだけです。新しいファイル)。

古い形式:

[settings]
email_to=people
special_websurf_processing=1
period_0_1_only=1
crc_recheck=0

[macro]
%::WebSurfRules =
    (
    'Step1' =>
        {
        action                  => 'NAVIGATE',
        inputstring             => 'http://www.tda-sgft.com/TdaWeb/jsp/fondos/Fondos.tda',
        },
    'Step2' =>
        {
        action        => 'CLICK_REFERENCE',
        matchtype     => 'OUTER',
        matchstring   => 'phHttpDest->\{\'FirstClick\'\}',
        pass          => 'phHttpDest->\{\'Step2Pass\'\}',
        },
    'Step3' =>
        {
        action        => 'CLICK_REFERENCE',
        matchtype     => 'OUTER',
        matchstring   => 'phHttpDest->\{\'SecondClick\'\}',
        },
    'Step4' =>
        {
        action        => 'CLICK_REFERENCE',
        matchtype     => 'OUTER',
        matchstring   => 'phHttpDest->\{\'DealClick\'\}',
        accept_multi_match  => 'ANY_TOP_FIRST',
        },
    'Step5' =>
        {
        action        => 'CLICK_REFERENCE',
        matchtype     => 'INNER',
        matchstring   => 'phHttpDest->\{\'LinkClick2\'\}',
        fail          => 'Step6',
    #    accept_multi_match  => 'ANY_TOP_LAST',
        },
    'Step6' =>
        {
        action        => 'CLICK_REFERENCE',
        matchtype     => 'INNER',
        matchstring   => 'phHttpDest->\{\'DocClick\'\}',
        },
    'Step7' =>
        {
        action                  => 'CLICK_DOWNLOAD_OK',
        },
    );

[data]
Print WebAddress______________  Destination_________________________________________________ FirstClick_________________ SecondClick________________    DealClick_________________________   LinkClick2________________________  DocClick___________________________________ PayInterval   DueDay  Step2Pass__________     QaRule_________________________________________________________________________________________________________________
0     http://www.tda-sgft.com/  d:\\$YYYYMM{$n}\\raw\\remit\\wl\\CXPEN1_apl.pdf              Mortgage Loan               ABS                            Caixa Penedes 1 TDA                  MAINPAGE - FAIL                     Fund´s Allocation                           q1                    Step3                   qa_regexp=Report D?d?ate\\s+\\d\\d\/$MM{$n}\/$YYYY{$n}
0     http://www.tda-sgft.com/  d:\\$YYYYMM{$n}\\raw\\remit\\wl\\CXPEN1_bond.pdf             Mortgage Loan               ABS                            Caixa Penedes 1 TDA                  MAINPAGE - FAIL                     Investors information on Payment Date       q1                    Step3                   qa_regexp=PAYMENT DATE:\\s+$aCAPMONTHNAMES[$MM{$n}-1].+$YYYY{$n}
0     http://www.tda-sgft.com/  d:\\$YYYYMM{$n}\\raw\\remit\\wl\\CXPEN1_bond.pdf             Mortgage Loan               ABS                            Caixa Penedes 1 TDA                  MAINPAGE - FAIL                     Investors information on Payment Date       q1                    Step3                   qa_regexp=PAYMENT DATE:\\s+$aCAPSHORTMONTHNAMES[$MM{$n}-1] \\d\\d.+? ?.? $YYYY{$n}
0     http://www.tda-sgft.com/  d:\\$YYYYMM{$n}\\raw\\remit\\wl\\CXPEN1_bond.pdf             Mortgage Loan               ABS                            Caixa Penedes 1 TDA                  MAINPAGE - FAIL                     Investors information on Payment Date       q1                    Step3                   qa_regexp=PAYMENT DATE:\\s+$aCAPMONTHNAMESSPANISH[$MM{$n}-1] \\d\\d.+? ?.? $YYYY{$n}

そして、私がそれを吐き出したいもの:

[settings]
email_to=people
special_websurf_processing=1
period_0_1_only=1
crc_recheck=0
[macro]
%::WebSurfRules =
    (
    '1'     => 'NAVIGATE,phHttpDest->\{\'WebAddress\'\}', 
    '2'     => 'CLICK_REFERENCE,phHttpDest->\{\'FirstClick\'\}',                                                         
    '3'     => 'CLICK_REFERENCE,phHttpDest->\{\'SecondClick\'\}',                                 
    '4'     => 'CLICK_REFERENCE,phHttpDest->\{\'DealClick\'\}',
    '5'     => 'CLICK_REFERENCE,phHttpDest->\{\'LinkClick2\'\}',                     
    '6'     => 'CLICK_REFERENCE,phHttpDest->\{\'DocClick\'\}',           
    );

[data]
Print WebAddress______________  Destination_________________________________________________ FirstClick_________________ SecondClick________________    DealClick_________________________   LinkClick2________________________  DocClick___________________________________ PayInterval   DueDay  Step2Pass__________     QaRule_________________________________________________________________________________________________________________
0     http://www.tda-sgft.com/  d:\\$YYYYMM{$n}\\raw\\remit\\wl\\CXPEN1_apl.pdf              Mortgage Loan               ABS                            Caixa Penedes 1 TDA                  MAINPAGE - FAIL                     Fund´s Allocation                           q1                    Step3                   qa_regexp=Report D?d?ate\\s+\\d\\d\/$MM{$n}\/$YYYY{$n}
0     http://www.tda-sgft.com/  d:\\$YYYYMM{$n}\\raw\\remit\\wl\\CXPEN1_bond.pdf             Mortgage Loan               ABS                            Caixa Penedes 1 TDA                  MAINPAGE - FAIL                     Investors information on Payment Date       q1                    Step3                   qa_regexp=PAYMENT DATE:\\s+$aCAPMONTHNAMES[$MM{$n}-1].+$YYYY{$n}
0     http://www.tda-sgft.com/  d:\\$YYYYMM{$n}\\raw\\remit\\wl\\CXPEN1_bond.pdf             Mortgage Loan               ABS                            Caixa Penedes 1 TDA                  MAINPAGE - FAIL                     Investors information on Payment Date       q1                    Step3                   qa_regexp=PAYMENT DATE:\\s+$aCAPSHORTMONTHNAMES[$MM{$n}-1] \\d\\d.+? ?.? $YYYY{$n}
0     http://www.tda-sgft.com/  d:\\$YYYYMM{$n}\\raw\\remit\\wl\\CXPEN1_bond.pdf             Mortgage Loan               ABS                            Caixa Penedes 1 TDA                  MAINPAGE - FAIL                     Investors information on Payment Date       q1                    Step3                   qa_regexp=PAYMENT DATE:\\s+$aCAPMONTHNAMESSPANISH[$MM{$n}-1] \\d\\d.+? ?.? $YYYY{$n}

それぞれのクリックでphHttpDestとアクションが、[data]セクションの見出しに関連付けられます。

4

1 に答える 1

2

したがって、これを行う1つの方法は、一連の正規表現置換を使用して、新しい形式でファイルを作成することです。私はあなたのフォーマットのルールを完全に理解していなかったので、私は一般的にすべてを実装しましたが、いくつかの違いがあります。あなたはそれを微調整するために行って、いくつかの調整をしなければならないでしょう。output.txtファイルは、例をinput.txtとして使用したときに生成されるファイルです。

コード

import re
data = open('input.txt').read()
data = re.sub(r"    'Step([0-9]+)' =>\s+{\s+action\s+=> ", r"    '\1'     => ", data)
data = re.sub(r"',\s+pass\s+[^,]+,", "", data)
data = re.sub(r"',\s+accept_multi_match\s+[^,]+,", "", data)
data = re.sub(r"\n +#.*\n", "\n", data)
data = re.sub(r"',\s+fail\s+[^,]+,", "", data)
data = re.sub(r"',\s+matchtype\s+[^,]+,", "", data)
data = re.sub(r"',\s+inputstring\s+=> '", ",", data)
data = re.sub(r"\s+matchstring\s+=> '", ",", data)
data = re.sub(r"\n        },", "',", data)
open('output.txt', 'w').write(data)

output.txt

[settings]
email_to=people
special_websurf_processing=1
period_0_1_only=1
crc_recheck=0

[macro]
%::WebSurfRules =
    (
    '1'     => 'NAVIGATE,http://www.tda-sgft.com/TdaWeb/jsp/fondos/Fondos.tda',',
    '2'     => 'CLICK_REFERENCE,phHttpDest->\{\'FirstClick\'\}',
    '3'     => 'CLICK_REFERENCE,phHttpDest->\{\'SecondClick\'\}',',
    '4'     => 'CLICK_REFERENCE,phHttpDest->\{\'DealClick\'\}',
    '5'     => 'CLICK_REFERENCE,phHttpDest->\{\'LinkClick2\'\}',
    '6'     => 'CLICK_REFERENCE,phHttpDest->\{\'DocClick\'\}',',
    '7'     => 'CLICK_DOWNLOAD_OK',',
    );

...
于 2012-11-23T17:49:04.470 に答える