以下の小さなコードを使用すると、ファイルを読み取って musicStyle フィールドを見つけ、値を別のものに置き換えて、ファイルに変更を書き込むことができます。それはxmlであり、lxmlまたは他のxmlパーサーを使用できることはわかっていますが、音楽コレクションの個人データベース用であり、それほど大きなものではないため、reモジュールを引き続き使用したいと考えています。
import re
def replace(theFile):
#for iLine in (line.rstrip('\n') for line in open(theFile,"w")):
if iLine.find("musicStyle") >= 0:
mpr = re.search(r'(.*>)(.*?)(<.*)', iLine, re.M|re.I)
print mpr.group(2)
# here goes the code to replace the mpr.group(2)
# should i use iLine.replace('rock','Metal') ?
if __name__ == '__main__':
replace('c:\testfile.xml')
前もって感謝します。