2011-08-05 26 views
1

問題,我昨天在這裏發表問題:Finding and adding to a .kml file using python的Python,修改文本文件,搜索方面

我讀了一堆教程,現在有了更好的理解蟒蛇,這是很好的。但我似乎仍然無法讓我的劇本正確。我知道我非常接近。基本上我想添加一堆JPG到一個.kml文件,它基本上是谷歌地球上的.xml文件。我希望我的程序能夠在名爲 的XML文件中找到谷歌地球「地標」:TO-XXX

其中XXX與TO-XXX.jpg匹配。我已經有一個包含一堆.jpgs的文件夾,其文件名與每個地標的名稱相匹配。我需要的程序找到

<name> (for example <name>TO-101</name>) 

,並添加正確的用名行下方的行:

<description> <img src=TO-101.jpg></description>. 

所以,我的代碼寫的,但我似乎無法得到它找到了。這始終是寫:

"\t\t\t<name>TO-XXX</name>\n". 

所以,這裏是代碼:

import os 

infile = 'TO-Hand-Holes2.kml' # the file I am reading 
outfile = 'TO-Hand-Holes-Output.kml' # the file I plan to write to, using print for now 
images = os.listdir("./images") # the images folder, all image names match names 

source = open(infile, 'r') 
target = open(outfile, 'w') 

x = 0 #an incrementer 
i = 0 # an incrementer 

readxml = source.readline 
while x < 20000: #There are about 17,000 lines in the .kml file 
    readxml = source.readline() 
    while i < len(images): 
     word = images[i] 
     if readxml == "\t\t\t<name>%s</name>\n" % word[:6]: #!!!!!!!!! the problem is here 
      print readxml #output the <name> 
      print word[:6] #output the <description> 
      hit = 'true' 
      i = i + 1 
     else: 
      hit = 'false' 
      #print "test%s" % word[:6] 
      i = i + 1 
    if hit == 'false': 
     print ("%s") % readxml 
    x = x + 1 

我似乎無法得到它的識別線。有什麼建議麼?

+0

可以保證格式/縮進顯示是你實際上使用的是什麼?如果是這樣,這應該不是真的很有用。你的第一個while循環沒有任何執行(它下面沒有縮進塊)。另外,如果它的設置與上面一樣,則在嘗試處理其中的任何一行之前,請先閱讀20000行。 –

+0

oops,它沒有粘貼正確,其餘的代碼行實際上是在while renosis

+0

當你說不識別時,你的意思是'readxml =='上的匹配嗎?\ t \ t \ t %s \ n「%word [:6]'不工作或者沒有輸出?在後一點上,我沒有看到你寫入輸出文件的任何地方。你需要像這樣的東西:'print >> target,word [:6]',假設python 3.0之前。 – hughdbrown

回答

1

因爲縮進是python中的語法,所以你需要注意事物的位置真的。這可能會更接近。這不是100%完成,但它會指向你在正確的方向:

with open(infile) as fhi, open(outfile, 'w') as fho: 
    for line in fhi: 
    if line == 'myMatchString': 
     fho.write(line.replace('this', 'that')) 

使用的with聲明,這是在2.7引入了多個文件的語法。在2.7之前,你必須嵌套第二個with以獲得第二個文件。

+0

謝謝......我明白了! – renosis

1

我做了一些改動,但是我沒有文件去測試它。爲了您的學習目的,我做了一些與Python相關的更改。我認爲你應該測試你想要的信息是否在字符串中,而不是檢查等價性。如果你想檢查等價性,你應該使用line.strip(),因爲它將包含你可能沒有考慮到的標籤,換行符等等(你不想明確說明tbh)。

import os 

infile = 'TO-Hand-Holes2.kml' # the file I am reading 
outfile = 'TO-Hand-Holes-Output.kml' # the file I plan to write to, using print for now 
images = os.listdir("./images") # the images folder, all image names match names 

source = open(infile, 'r') 
target = open(outfile, 'w') 

for line in source.readlines(): #read all of the source lines into a list and iterate over them 
    for image in images: #you can iterate over a list like this 
     word = image[:6] #i moved the list slicing here so you only have to do it once 
     if "<name>" in line and word in line: #!!!!!!!!! the problem is here 
      print line #output the <name> 
      print word #output the <description> 
      hit = True #use the built-in Python boolean type 
     else: 
      hit = False 
      #print "test%s" % word[:6] 
     target.write(line) 
     if hit: 
      target.write("<description> <img src={0}></description>\n".format(image) 

source.close() 
target.close() 
+2

不要在source.readlines()中使用'for line。它將整個文件讀入內存,這是沒有理由的。文件句柄是他們自己的迭代器。你應該使用'for line in source'。 –

+0

好的....我正在嘗試這個。它似乎在讀取文件。我現在很困惑。呵呵。基本上我需要源文件與目標文件完全相同,除了在具有 TO-XXX的每行之下添加。所以,由於我缺乏經驗,我確信要做到這一點的唯一方法是讀取文件中的每一行,寫入每一行,但如果出現了TO-XXX,還要寫一個作爲下一行並繼續。 – renosis

+0

而且我知道我的代碼不會將標籤放在那裏。在我確認搜索和添加行部分正在工作後,我將在稍後添加該部分。 – renosis

1

這會更好:

import os 

infile = 'TO-Hand-Holes2.kml' # the file I am reading 
outfile = 'TO-Hand-Holes-Output.kml' # the file I plan to write to, using print for now 
images = os.listdir("./images") # the images folder, all image names match names 


with open(infile, 'r') as source: 
    with open(outfile, 'w') as target: 
     for readxml in source: 
      for word in images: 
       hit = readxml == "\t\t\t<name>%s</name>\n" % word[:6] 
       if hit: #!!!!!!!!! the problem is here 
        print readxml #output the <name> 
        print >> target, word[:6] #output the <description> 
+0

我最終做了類似這樣的事情......謝謝! – renosis