2016-12-21 8 views
-1

テキストファイルcran.all.1400で作業する必要があります。python - 半構造化テキストを解析する方法(cran.all.1400)

これは記事の要約の集まりで、各記事に関するいくつかの追加データがあります。その形態で:

.I 1
.T
スリップストリームにおける翼の空力特性の実験的研究。
.A
brenckman、m。
.B
j。 ae。 scs。 25、1958年、324
.W
//テキストの多くは小さな 粘度の非圧縮性流体中の平板過去
.I 2
.T
単純せん断流れ。
.A
航空工学のティン・伊利
.B
部門、工芸 研究所 トロイ、ニューヨーク州のレンセラー
.W
//ようにテキスト


の多くと。私は必要なもの

はそうのような編成されたデータである。

記事1:.T = "どんな物品1のタイトルがある"、.A = "E著者がある/ W"、.B =」 .T = "すべてのテキスト"
記事2:.T = "タイトルが何であれ"、.A = "作成者である"、.B = "w/e"、。 T = "すべてのテキスト"

私はこれをPythonでどうやってやりますか? ありがとうございます。

+2

あなたは何を試しましたか? [質問ルールを読む] – MYGz

+2

これは、ドットと大文字とオプションの属性だけで構成されたキーワードが行と通常の行にあるように見えます。ファイルを1行ずつ処理して、あなたがどこかでつかまえている場合は、より正確な質問をするためにここに来てください。 –

+0

私はファイル全体を単一の文字列(読み込み時)として読み込み、.Iを区切り文字として使用して文字列を分割しようとしました。それは私に記事のリストを与えました(最初は空の要素がありますが、私はそれを管理できます)。今では、他のタグ/キーワードで記事を分割する必要がありますが、どの要素がどの記事に属しているかはまだ分かります。私は辞書やテーブル/ 2D配列の辞書が必要だと思います。 – Car

答えて

0

.Iの分割のコメントからのあなたの考えは良いスタートのようです。

以下が動作するように思われる:

with open('crantest.txt') as f: 
    articles = f.read().split('\n.I') 

def process(i, article): 
    article = article.replace('\n.T\n','.T=') 
    article = '.T=' + article.split('.T=')[1] #strips off the article number, restored below 
    article = article.replace('\n.A\n',',.A=') 
    article = article.replace('\n.B\n',',.B=') 
    article = article.replace('\n.W\n',',.W=') 
    return 'article ' + str(i) + ':' + article 

data = [process(i+1, article) for i,article in enumerate(articles)] 

私は最初の10回の記事(小ヘッダと.I 11始まるファイルの全てを廃棄する)からなるテストファイルを作成しました。上記のコードを実行すると、長さ10のリストが得られます。最初の行が.I(前の改行なし)で始まることが重要です。分割の最初のエントリが空であるかどうかをテストする必要がないからです。リストの最初のエントリから始まる文字列です:

article 1:.T=experimental investigation of the aerodynamics of a\nwing in a slipstream .,.A=brenckman,m.,.B=j. ae. scs. 25, 1958, 324.,.W=experimental investigation of the aerodynamics of a\nwing in a slipstream 

編集にここでは、連続的に関連するチャンクのプルするpartitionを使用する辞書のバージョンです。これは、辞書の辞書ではなく、文字列のリストを返す:

with open('crantest.txt') as f: 
    articles = f.read().split('\n.I') 

def process(article): 
    article = article.split('\n.T\n')[1] 
    T, _, article = article.partition('\n.A\n') 
    A, _, article = article.partition('\n.B\n') 
    B, _, W = article.partition('\n.W\n') 
    return {'T':T, 'A':A, 'B':B, 'W':W} 

data = {(i+1):process(article) for i,article in enumerate(articles)} 

例えば:

>>> data[1] 
{'A': 'brenckman,m.', 'T': 'experimental investigation of the aerodynamics of a\nwing in a slipstream .', 'B': 'j. ae. scs. 25, 1958, 324.', 'W': 'experimental investigation of the aerodynamics of a\nwing in a slipstream .\n an experimental study of a wing in a propeller slipstream was\nmade in order to determine the spanwise distribution of the lift\nincrease due to slipstream at different angles of attack of the wing\nand at different free stream to slipstream velocity ratios . the\nresults were intended in part as an evaluation basis for different\ntheoretical treatments of this problem .\n the comparative span loading curves, together with\nsupporting evidence, showed that a substantial part of the lift increment\nproduced by the slipstream was due to a /destalling/ or\nboundary-layer-control effect . the integrated remaining lift\nincrement, after subtracting this destalling lift, was found to agree\nwell with a potential flow theory .\n an empirical evaluation of the destalling effects was made for\nthe specific configuration of the experiment .'} 

デリミタの最初に出現する前に文字列sの一部からなる三重を返しs.partition()、区切り文字そのもの、およびその区切り文字が出現した後の文字列の一部です。コード内のアンダースコア(_)は、意図が戻り値のその部分を破棄することを強調するPythonイディオムです。

+0

これは私が必要とするものにとても近いです。しかし、私の質問は十分ではなかった、私は恐れている。ストリングも壊れている必要があります。私は、例えば7番目の記事の.Wの部分にアクセスできる、何らかのデータ構造が必要です。並べ替えのリストまたは何か。私はまだ私がそれを明確にしているかどうかはわかりません。しかし、「私が列挙した記事(記事)」は大きな助けとなります。私はそれを使用する必要がある場所に到達すると思います。ありがとうございました! – Car

+0

辞書のリストが好きなように聞こえます:記事ごとに1つの辞書があり、各辞書にはキー「T」、「A」、「B」、および「W」があります。 –

+0

はい、それはまさに正しいと思います! :)私はあなたのコードを今日後で行うように修正しようとします。最初に終わらなければならないことがあります。お手伝いありがとう! – Car

関連する問題