2016-11-12 41 views
-1

我有一个文本文件,它是放在一起的几个XML文件。目标是将文本分成多个XML。从字符串列表中创建XML文件

我已经在这个片段中的代码来

def split_file(filename): 
    """ 
    Split the input file into separate files, each containing a single patent. 
    As a hint - each patent declaration starts with the same line that was 
    causing the error found in the previous exercises.  
    """ 
    f = open(filename, 'r').read().split('\n') 
    last_header_line = 0 
    counter_q_of_files = 0  
    for line in enumerate(f) : 
     lines_ls = [] 
     ## add that line to an object that will be converted in xml file on the next condition 
     ## code here 
     if line[1] == '<?xml version="1.0" encoding="UTF-8"?>': 
      ## Make an xml file out of the previously created object, from lines_ls[last_header_line:line[0]] 
      ## code here 
      last_header_line = line[0] 
      counter_q_of_files = counter_q_of_files+1 

我可以建立串(每未来XML行一个元素)的列表,该列表转换成XML文件?如果是,如何?

+0

请修正您的代码的缩进。在函数名下,所有的代码都应该在函数内部。所以请修复你的缩进。 –

+0

这是好吗? @cco –

+0

感谢您的时间,现在我认为它是@cco –

回答

0

如果你有很多的内存,你可以分割'<?xml version="1.0" encoding="UTF-8"?>'而不是'\n'

如果您想积累行数,然后了解列表,尤其是关于append方法。字符串的join方法也是有用的。您在代码中使用enumerate并不正确,但我认为它不会有帮助。

+0

我知道附加的方法。问题是: 如果我追加每一行,并以行列表结束,如何获取该列表并使其成为一个XML文件? –

+0

如果您正在阅读美国PTO数据(我怀疑您可能会这样做),那么您所要做的就是使用''\ n''加入这些行,它将是一个有效的XML文件(它实际上并不是需要''\ n''才能成为有效的XML,但它可能需要它们拥有正确的内容)。 – cco

+0

感谢您的评论,但这不是我所期待的。我设法使用以下代码: '#从line_ls的内容中制作一个字符串,从last_header_line到line [0] str_lines ='\ n'.join(lines_ls [last_header_line:line [0]])输出文件: output_file.write(str_lines)'输出文件的格式为 new_file_name =“{} - {}”格式(文件名,counter_q_of_files) (open_new_file_name,“wb” –