テキスト ファイルからテキストを読み取り、そのテキストを再フォーマットして別のテキスト ファイルに書き込みます。
私が読んでいるテキストは次のとおりですtestFile.txt
。
*******************************
* Void Fractions in the Bed *
*******************************
Z(m) MIN.FLUIDIZ. EMULSION TOTAL
0.0000E+00 0.4151E+00 0.8233E+00 0.8233E+00
0.1000E-09 0.4151E+00 0.8233E+00 0.8233E+00
0.1000E-05 0.4151E+00 0.8233E+00 0.8233E+00
0.2000E-05 0.4151E+00 0.8233E+00 0.8233E+00
0.1251E+01 0.4151E+00 0.9152E+00 0.9152E+00
0.1301E+01 0.4151E+00 0.9152E+00 0.9152E+00
0.1333E+01 0.4151E+00 0.9152E+00 0.9152E+00
*************************************
* Void Fractions in the Freeboard *
*************************************
Z(m) VOID FRACTION
0.1333E+01 0.9992E+00
0.1333E+01 0.9992E+00
0.1333E+01 0.9992E+00
0.1333E+01 0.9992E+00
0.3533E+01 0.9992E+00
0.3633E+01 0.9992E+00
0.3733E+01 0.9992E+00
0.3833E+01 0.9992E+00
0.3933E+01 0.9992E+00
0.4000E+01 0.9992E+00
*********************************************
* Superficial Velocities in the Bed (m/s) *
*********************************************
Z(m) MIN.FLUIDIZ. ACTUAL
0.0000E+00 0.1235E+00 0.4911E+01
0.1000E-09 0.1235E+00 0.4911E+01
0.1000E-05 0.1235E+00 0.4911E+01
0.2000E-05 0.1235E+00 0.4911E+01
0.3000E-05 0.1235E+00 0.4911E+01
0.1151E+01 0.1235E+00 0.4915E+01
0.1201E+01 0.1235E+00 0.4915E+01
0.1251E+01 0.1235E+00 0.4915E+01
0.1301E+01 0.1235E+00 0.4915E+01
0.1333E+01 0.1235E+00 0.4915E+01
以下は、テキスト ファイルを解析するための Python コードです。
openFile = open('testFile.txt','r')
groupOneFile = open('groupOneFile.csv','w')
groupTwoFile = open('groupTwoFile.csv','w')
groupThreeFile = open('groupThreeFile.csv','w')
idx = 0;
firstIdx = 0;
secondIdx = 0;
thirdIdx = 0;
for line in openFile:
# first group
if '* Void Fractions in the Bed *' in line:
print line
firstIdx = idx
if idx in range(firstIdx+5,firstIdx+43):
line = line.lstrip()
line = line.replace(' ',',')
groupOneFile.write(line)
# second group
if '* Void Fractions in the Freeboard *' in line:
print line
secondIdx = idx
if idx in range(secondIdx+5,secondIdx+43):
line = line.lstrip()
line = line.replace(' ',',')
groupTwoFile.write(line)
# third group
if '* Superficial Velocities in the Bed (m/s) *' in line:
print line
thirdIdx = idx
if idx in range(thirdIdx+5,thirdIdx+43):
line = line.lstrip()
line = line.replace(' ',',')
groupThreeFile.write(line)
idx += 1
openFile.close()
groupOneFile.close()
groupTwoFile.close()
groupThreeFile.close()
groupOneFile
には、次のデータが含まれている必要があります。
0.0000E+00,0.4151E+00,0.8233E+00,0.8233E+00
0.1000E-09,0.4151E+00,0.8233E+00,0.8233E+00
0.1000E-05,0.4151E+00,0.8233E+00,0.8233E+00
0.2000E-05,0.4151E+00,0.8233E+00,0.8233E+00
0.1251E+01,0.4151E+00,0.9152E+00,0.9152E+00
0.1301E+01,0.4151E+00,0.9152E+00,0.9152E+00
0.1333E+01,0.4151E+00,0.9152E+00,0.9152E+00
groupTwoFile
には次のものが必要です。
0.1333E+01,0.9992E+00
0.1333E+01,0.9992E+00
0.1333E+01,0.9992E+00
0.1333E+01,0.9992E+00
0.3533E+01,0.9992E+00
0.3633E+01,0.9992E+00
0.3733E+01,0.9992E+00
0.3833E+01,0.9992E+00
0.3933E+01,0.9992E+00
0.4000E+01,0.9992E+00
についても同様groupThreeFile
です。
本文ファイルの読み込みと他のファイルへのデータの書き込みは正常に動作しています。問題は、 に書き込まれるデータがgroupOneFile
、他のファイルgroupTwoFile
およびの先頭にも書き込まれることですgroupThreeFile
。どうすればこれを防ぐことができますか?