0

次のような何千ものエントリを含むテキストファイルがあります。

@INBOOK{Abu-Lughod1991,
  chapter = {Writing against culture},
  pages = {137-162},
  title = {Recapturing anthropology},
  publisher = {School of American Research Press},
  year = {1991},
  editor = {Richard Fox},
  author = {Abu-Lughod, Lila},
  address = {Santa Fe /NM},
  abstract = {Im Zusammenhang mit der Debatte um die writing culture fomuliert AL
        eine feministische Kritik und zeigt, wie von dort doch Anregungen
        für die Reflektion der Schreibweise und Repräsentation gekommen sind.*},
  crossref = {Rabinow1986},
  keywords = {Frauen; Feminismus; Erzählung als EG; Repräsentation; Roman; Schreibtechnik;
        James Clifford; writing culture; Dialog;},
  owner = {xko},
  systematik1 = {Anth\theor\Ethnographie},
  systematik2 = {Anth\theor\Text & Ges},
  timestamp = {1995-12-02}
}

キーワード フィールドのすべてのセミコロンをコンマに置き換えます。ただし、キーワード フィールドのみ - 他のフィールドは変更しないでください。

keywords = {Frauen, Feminismus, Erzählung als EG, Repräsentation, Roman, Schreibtechnik, James Clifford, writing culture, Dialog,},

私はプログラマーではありませんが、次のコード スニペットは良い出発点になるかもしれません。

outfile = open("literatur_comma.txt", "w") 
for line in open("literatur_semicolon.txt", "r"): 
    if line  # starts with "keywords" replace all semicolon with comma
        outfile.write(line) # write in new file
outfile.close() 

どうもありがとう!

編集:すべての回答とコードをありがとう、それは素晴らしいです! 私は自分の考えを間違えました。コードラッパーを (outfile とともに) 使用すると、キーワードを含む新しいファイルが作成されます。同じファイルを使用して、キーワード行のセミコロンのみをコンマに置き換えるにはどうすればよいですか?

4

3 に答える 3

3

このようなものは、1行で機能します。

if line.strip().startswith('keywords'):
    line = line.replace(';',',')
outfile.write(line) 

ただし、キーワードが実際のテキストファイルの複数行にまたがっている場合、これでは作業は完了しません。

于 2012-11-05T17:10:56.570 に答える
0
outfile = open("literatur_comma.txt", "w") 
for line in open("literatur_semicolon.txt", "r"): 
    if line.startswith('keywords'):  # starts with "keywords" replace all semicolon with comma
        outfile.write(line.replace(';',',')) # write in new file
outfile.close() 
于 2012-11-05T17:11:45.827 に答える
0

パイパーシングの使用

注:これはそれを行う1つの方法ですが、脳は解析モードではありません-したがって、これは適切な答えではなくアイデアです...確かにいくつかの作業が必要ですが、正しい方向かもしれません...

...を使用したやや面倒な例pyparsing(いくつかの @INBOOK と wotsit のチェックと解析を使用すると、はるかに優れたものになる可能性がありますが、とにかく...)

from pyparsing import *

keywords = originalTextFor(Keyword('keywords') + '=')
values = delimitedList(Regex('[^;}]+'), ';')
values.setParseAction(lambda L: ', '.join(L))

あなたの例はどこですかtext

>>> print values.transformString(text)
@INBOOK{Abu-Lughod1991,
  chapter = {Writing against culture},
  pages = {137-162},
  title = {Recapturing anthropology},
  publisher = {School of American Research Press},
  year = {1991},
  editor = {Richard Fox},
  author = {Abu-Lughod, Lila},
  address = {Santa Fe /NM},
  abstract = {Im Zusammenhang mit der Debatte um die writing culture fomuliert AL
        eine feministische Kritik und zeigt, wie von dort doch Anregungen
        für die Reflektion der Schreibweise und Repräsentation gekommen sind.*},
  crossref = {Rabinow1986},
  keywords = {Frauen, Feminismus, Erzählung als EG, Repräsentation, Roman, Schreibtechnik, James Clifford, writing culture, Dialog;},
  owner = {xko},
  systematik1 = {Anth   heor\Ethnographie},
  systematik2 = {Anth   heor\Text & Ges},
  timestamp = {1995-12-02}
于 2012-11-05T17:54:21.473 に答える