Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:从输入文件(xml文件)提取数据时出错,循环在一些迭代后停止_Python_Itertools_Extraction - Fatal编程技术网

Python:从输入文件(xml文件)提取数据时出错,循环在一些迭代后停止

Python:从输入文件(xml文件)提取数据时出错,循环在一些迭代后停止,python,itertools,extraction,Python,Itertools,Extraction,我有一个XML文件,看起来像这样, 该文件包含5000个配置文件(数据集),每个配置文件包含92行和5列,每个配置文件由2行分隔(我想跳过)。 我想提取一些选定的配置文件并写入另一个文件。 但是有了这段代码,我只能提取有限的概要文件 with open('file.xml') as f: for j in lat : l=94*j i=l-92 g.write('%s' % j) g.write(":-profi

我有一个XML文件,看起来像这样, 该文件包含5000个配置文件(数据集),每个配置文件包含92行和5列,每个配置文件由2行分隔(我想跳过)。 我想提取一些选定的配置文件并写入另一个文件。 但是有了这段代码,我只能提取有限的概要文件

    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")
以open('file.xml')作为f的
:
对于lat中的j:
l=94*j
i=l-92
g、 写入(“%s”%j)
g、 写入(“:-profile”)
g、 写入(“\n”)
对于itertools.islice(f,i,l)中的线路:
g、 写入(“%s”%行)
g、 写(“”)
g、 写入(“\n”)
g、 写(“”)
g、 写入(“\n”)
当我打印“j”时,它占用了“lat”(我选择的配置文件)的所有值。 在我的输出文件中,我得到的值最多只有几个概要文件,之后它只显示最后一行

        g.write("</Matrix>")
        g.write("\n")
        g.write('<Matrix nrows="92" ncols="5">')
        g.write("\n")
g.write(“”)
g、 写入(“\n”)
g、 写(“”)
g、 写入(“\n”)
我知道这很愚蠢,但我是python编程的初学者。请帮助我

我尝试将“j”和“line”一起打印,经过某些迭代后,输出仅显示j的值,没有行的输出
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)
nums_profiles=set() 打开(“lat_sel.dat”、“r”)作为num_profiles_文件: 对于num_profiles_file.readlines()中的行: 对于行中的i.split(): nums_profiles.add(int(i)) 使用open('extracted_output.xml','w')作为输出_文件,open('chevallierl91_clear_q.xml','r')作为矩阵_文件: 配置文件\u计数器=0 对于矩阵_文件.readlines()中的行: #保存结束的xml标记 对于['',''中的结束标记: 如果end_标记位于行中: 输出_文件。写入(行) #计数配置文件 如果“矩阵nrows”在同一行: 配置文件_计数器+=1 #保存xml文件的头 如果profile_计数器==0:
如果“您能显示
j
(即
print(j)
)的输出)的值吗?它是这样的:-6 8 10 11 24 25 27 28 36 42 44 54 59 60 62 65 67 68 69 80 81 90 92 94 102 103 109 111 115 116。。。。。。49394941494349454947495049534954495549574958496049614962496449724974497849794980498249874989499149934996总共有2037个值一切似乎都正常。如果没有最少的可重复代码和数据,就很难提出任何建议。您说过每个配置文件由两行分隔,我不知道代码是如何检查这两行的。我将尝试解释,每个配置文件都以两行HTML代码开始,然后是我的92行,因此总共有94行用于一个配置文件。在第一次迭代中,j取值6,代码将在第564行(94*6)和第472行(564-92)之间读取,这就是我应用的逻辑。这有错误吗??谢谢如果这对您来说不是问题,您可以临时将您的源文件上载到github,然后我可以找到问题所在并为您解决。@caty,如果您接受答案,我将不胜感激:)