それなら、
line = 'xxxx'
CONFIG = default_deconfig
if line == 'AAA':
CONFIG = AAA_defconfig
elif line == 'CCC':
CONFIG = CCC_defconfig
...
これがPythonファイルではなく、操作したい場合を除きます。そのように見えます。
その場合、行変数に基づいて構成ファイルを作成する構成ジェネレーターを作成します。
[編集:コメントに基づく]
いくつかの調整が必要になる場合がありますが、これは機能するはずです。
# Simple , crude solution
f = open('file1', 'r')
manipulated_lines = []
readFirstLine = False
config = ''
configComma = ''
uncommentLine = 0
for line in f:
tokens = line.split()
if uncommentLine == 1:
# this is comment line
if tokens[0] == '#export':
manipulated_lines.append(line[1:])
uncommentLine = uncommentLine + 1
continue
elif uncommentLine > 1:
manipulated_lines.append(line)
continue
if not readFirstLine:
config = line.rstrip('\n')
configComma = config + ','
readFirstLine = True
# Process additional lines
manipulated_lines.append(line)
if len(tokens) > 0 and tokens[0] == '#':
if tokens[1] == 'for':
if config in tokens or configComma in tokens:
uncommentLine = uncommentLine + 1
continue
print manipulated_lines
f.close()
fw = open('file2', 'w')
fw.writelines(manipulated_lines)
fw.close()
入力:file1
CCC
# for AAA
#export CONFIG = AAA_defconfig
# for BBB, BbB, Bbb, BBb3
#export CONFIG = BBB_defconfig
# for CCC, CcC, Ccc, Ccc1
#export CONFIG = CCC_defconfig
...
出力:file2
CCC
# for AAA
#export CONFIG = AAA_defconfig
# for BBB, BbB, Bbb, BBb3
#export CONFIG = BBB_defconfig
# for CCC, CcC, Ccc, Ccc1
export CONFIG = CCC_defconfig
...