Python:Error 从输入文件(xml 文件)中提取数据,循环在一些迭代后停止
Python:Error in extracting data from input file(xml file),Loop stops after some iterations
我有一个 XML 文件,看起来像这样,example
该文件包含 5000 个配置文件(数据集),每个配置文件包含 92 行和 5 列,每个配置文件由 2 行分隔(我想跳过)。
我想提取一些选定的配置文件并写入另一个 file.I 已经制作了以下程序来执行此操作。
但是使用这段代码,我只能提取有限的配置文件。
with open('file.xml') as f:
for j in lat :
l=94*j
i=l-92
g.write('%s' % j)
g.write(":-profile")
g.write("\n")
for lines in itertools.islice(f, i, l):
g.write('%s' % lines)
g.write("</Matrix>")
g.write("\n")
g.write('<Matrix nrows="92" ncols="5">')
g.write("\n")
当我打印 'j' 时,它占用了 'lat'(我选择的配置文件)的所有值。
在我的输出文件中,我只获得了几个配置文件的值,之后它只显示最后几行
g.write("</Matrix>")
g.write("\n")
g.write('<Matrix nrows="92" ncols="5">')
g.write("\n")
我知道这很傻,但我是 python 编程的初学者..请帮忙
我尝试一起打印 'j' 和'lines',经过某些迭代后输出仅显示 j 的值,没有输出 lines
import re
nums_profiles = set()
with open("lat_sel.dat", "r") as num_profiles_file:
for line in num_profiles_file.readlines():
for i in line.split():
nums_profiles.add(int(i))
with open('extracted_output.xml', 'w') as output_file, open('chevallierl91_clear_q.xml', "r") as matrix_file:
profile_counter = 0
for line in matrix_file.readlines():
# save the ending xml tags
for end_tag in ['</Array>', '</arts>']:
if end_tag in line:
output_file.write(line)
# counting profiles
if 'Matrix nrows' in line:
profile_counter += 1
# save header of xml file
if profile_counter == 0:
if '<Array type="Matrix" nelem=' in line:
line = re.sub('nelem="[0-9]+"', 'nelem="%s"', line) % len(nums_profiles)
output_file.write(line)
# check if profile is the one which we need. If so, save data
if profile_counter in nums_profiles:
output_file.write(line)
我有一个 XML 文件,看起来像这样,example 该文件包含 5000 个配置文件(数据集),每个配置文件包含 92 行和 5 列,每个配置文件由 2 行分隔(我想跳过)。 我想提取一些选定的配置文件并写入另一个 file.I 已经制作了以下程序来执行此操作。 但是使用这段代码,我只能提取有限的配置文件。
with open('file.xml') as f:
for j in lat :
l=94*j
i=l-92
g.write('%s' % j)
g.write(":-profile")
g.write("\n")
for lines in itertools.islice(f, i, l):
g.write('%s' % lines)
g.write("</Matrix>")
g.write("\n")
g.write('<Matrix nrows="92" ncols="5">')
g.write("\n")
当我打印 'j' 时,它占用了 'lat'(我选择的配置文件)的所有值。 在我的输出文件中,我只获得了几个配置文件的值,之后它只显示最后几行
g.write("</Matrix>")
g.write("\n")
g.write('<Matrix nrows="92" ncols="5">')
g.write("\n")
我知道这很傻,但我是 python 编程的初学者..请帮忙
我尝试一起打印 'j' 和'lines',经过某些迭代后输出仅显示 j 的值,没有输出 lines
import re
nums_profiles = set()
with open("lat_sel.dat", "r") as num_profiles_file:
for line in num_profiles_file.readlines():
for i in line.split():
nums_profiles.add(int(i))
with open('extracted_output.xml', 'w') as output_file, open('chevallierl91_clear_q.xml', "r") as matrix_file:
profile_counter = 0
for line in matrix_file.readlines():
# save the ending xml tags
for end_tag in ['</Array>', '</arts>']:
if end_tag in line:
output_file.write(line)
# counting profiles
if 'Matrix nrows' in line:
profile_counter += 1
# save header of xml file
if profile_counter == 0:
if '<Array type="Matrix" nelem=' in line:
line = re.sub('nelem="[0-9]+"', 'nelem="%s"', line) % len(nums_profiles)
output_file.write(line)
# check if profile is the one which we need. If so, save data
if profile_counter in nums_profiles:
output_file.write(line)