gpt4 book ai didi

Python write() 函数将先前的数据写入下一个文件

转载 作者:行者123 更新时间:2023-12-01 05:32:17 25 4
gpt4 key购买 nike

我正在从文本文件中读取文本,然后重新格式化该文本以写入不同的文本文件。

我正在阅读的文本如下: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 的数据也被写入其他文件 groupTwoFilegroupThreeFile 的开头。如何防止这种情况发生?

最佳答案

要使其正常工作,您只需初始化即可

firstIdx = 1000000
secondIdx = 1000000
thirdIdx = 1000000

因为问题是如果你将它们设置为 0那么第一行将在所有组的范围内。

但请注意,这段代码效率非常低...更好的方法可能是:

outputFile = None

for line in openFile:
if '* Void Fractions in the Bed *' in line:
idx = 0; outputFile = groupOneFile
elif '* Void Fractions in the Freeboard *' in line:
idx = 0; outputFile = groupTwoFile
elif '* Superficial Velocities in the Bed (m/s) *' in line:
idx = 0; outputFile = groupThreeFile

if outputFile and 5 <= idx < 43:
line = line.lstrip()
line = line.replace(' ',',')
outputFile.write(line)

idx = idx + 1

在 Python 中,如果你写 if x in range(a, b):每次进行测试时都会对每个元素进行检查(或者在 Python 2.x 中,构建从 ab-1 的所有整数的实际列表)。更好的方法是将测试编写为 if a <= x < b: .

另请注意 2.5 in range(0, 10)会返回 false(当然 0 <= 2.5 < 10 是 true)。

在Python中没有switch语句,但您可以改为构建一个调度表:

filemap = [('*  Void Fractions in the Bed  *', groupOneFile),
('* Void Fractions in the Freeboard *', groupTwoFile),
('* Superficial Velocities in the Bed (m/s) *', groupThreeFile)]

outputFile = None
for line in openFile:
for tag, file in filemap:
if tag in line:
idx = 0
outputFile = file
if outputFile and 5 <= idx < 43:
outputFile.write(line)
idx += 1

如果可以精确匹配(而不是 in 测试),那么使用字典可以做得更好:

filemap = {'*  Void Fractions in the Bed  *': groupOneFile,
'* Void Fractions in the Freeboard *': groupTwoFile,
'* Superficial Velocities in the Bed (m/s) *': groupThreeFile)}

outputFile = None
for line in openFile:
f = filemap.get(line.strip())
if f:
# Found a new group header, switch output file
idx = 0
outputFile = f
if outputFile and 5 <= idx < 43:
outputFile.write(line)
idx += 1

关于Python write() 函数将先前的数据写入下一个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19942399/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com