2016-11-12 69 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」 –