2017-07-23 75 views
0

我在使用python編程時遇到了新問題,但是目前我收到了編寫一個寫入所有ID的腳本的任務,其中發生了type = 0或type = 1。它看起來像這個例子中的XML文件:通過文件夾中的文件打開文件

<root> 
<bla1 type="0" id = "1001" pvalue:="djdjd"/> 
<bla2 type="0" id = "1002" pvalue:="djdjd" /> 
<bla3 type="0" id = "1003" pvalue:="djdjd"/> 
<bla4 type="0" id = "1004" pvalue:="djdjd"/> 
<bla5 type="0" id = "1005" pvalue:="djdjd"/> 
<bla6 type="1" id = "1006" pvalue:="djdjd"/> 
<bla7 type="0" id = "1007" pvalue:="djdjd"/> 
<bla8 type="0" id = "1008" pvalue:="djdjd"/> 
<bla9 type="1" id = "1009" pvalue:="djdjd"/> 
<bla10 type="0" id = "1010" pvalue:="djdjd"/> 
<bla11 type="0" id = "1011" pvalue:="djdjd"/> 
<bla12 type="0" id = "1009" pvalue:="djdjd"/> 

<root> 

因此,代碼做的第一件事就是基本上替代:用「=」的原因,使我的XML上傳導致錯誤「=」。無論如何,它會記下ID的類型爲0,ID的類型爲1.這對於一個xml文件來說是完美的。不幸的是,我有更多的只有一個文件,我需要像循環一樣,總是打開文件夾中的下一個xml文件(不同的名稱),並將新的ID添加到最後一個xml中的ID中。所以基本上它總是添加新的xml文件中找到的新ID。

import xml.etree.cElementTree as ET # required import 

    XmlFile = 'ID3.xml' # insert here the name of the XML-file, which needs to be inside the same folder as the .py file 

    my_file = open('%s' % XmlFile, "r+") # open the XML-file 
    Xml2String = my_file.readlines() # convert the file into a list strings 

    XmlFile_new = [] # new list, which is filled with the modified strings 
    L = len(Xml2String) # length of the string-list 
    for i in range(1, L): # Increment starts at 0, therefore, the first line is ignored 
     if ':=' in Xml2String[i]: 
      XmlFile_new.append(Xml2String[i].replace(':=', '=')) # get rid of colon 
     else: 
      XmlFile_new.append(Xml2String[i]) 

    tree = ET.ElementTree(XmlFile_new) 
    root = tree.getroot() 

    id_0 = [] # list for id="0" 
    id_1 = [] # list for id="1" 
    id_one2zero = [] # list for ids, that occur twice 

    for i in range(len(root)): 
     if 'type="0"' in root[i]: # check for type 
      a = root[i].index("id") + 5 # search index of id 
      b = a+6 
      id_0.append((root[i][a:b])) # the id is set via index slicing 
     elif 'type="1"' in root[i]: # check for type 
      a = root[i].index("id") + 5 
      b = a+6 
      id_1.append((root[i][a:b])) 
     else: 
      print("Unknown type occurred") # If there's a line without type="0" or type="1", this message gets printed 
      # (Remember: first line of the xml-file is ignored) 

    for i in range(len(id_0)): # check for ids, that occur twice 
     for j in range(len(id_1)): 
      if id_0[i] == id_1[j]: 
       id_one2zero.append(id_0[i]) 
    print(id_0) 
    print(id_1) 
    f = open('write.xml','w') 
    print >>f, 'whatever' 
    print('<end>') 

回答

0

解決此問題的簡單方法是使用os.walk()函數。有了它,您可以在一個目錄中甚至遞歸地打開所有文件。

下面是一個例子,如何使用它:

for root, dirs, files in os.walk("your/path"): 
    for file in files: 
     # process your file 

如果您還有其他的文件比在你的目錄XML的文件,你可以用file.endswith(".xml")確保您只處理XML的文件。

+0

非常感謝您的評論!我會盡快測試它:-) – Mueller

+0

...所以我想我會擁有所有的xml文件......就像一個數組,對吧?我如何告訴python下一步然後繼續下一個等?你可以給我一個例子,如果在我的情況下,我有3 xml的,我想工作?可以說:ID1.xml,ID2.xml,ID3.xml在文件夾中。 XML的名稱不同,但結構始終相同。我需要循環嗎? – Mueller

+0

是的,你需要一個循環。我試圖在我的例子中解釋這個,我提供了一個可以工作的循環。 'os.walk()'返回一個文件列表(在你的情況下是'[「ID1.xml」,「ID2.xml」,「ID3.xml」]和'文件中的文件:'循環所有這些文件在列表中,無論你在哪裏寫下我的評論'#處理你的文件',都會爲所有文件執行 –