2015-02-11 209 views
-2

我的代碼有幾個問題。我瞭解其中一個,但不知道如何解決它。代碼是爲了讀取文本文件。讀/寫文件:按特定順序寫入行 - Python

格式的TXT文件:

204 jack sparrow 

http://testlink.com/test123 

123 Doughboy® 

http://testlink.com/test346 

348 ༺༃ོེċℏυƿᾰċᾰ♭Իᾰ༂ི༻ 

http://testlink.com/testr55 

等..

接下來應該寫另一個文件與輸出如下:

輸出文件格式:

204 http://testlink.com/test123&u_link=jack_sparrow 

123 http://testlink.com/test346&u_link=Doughboy® 

348 http://testlink.com/testr55&u_link=༺༃ོེċℏυƿᾰċᾰ♭Իᾰ༂ི༻ 

等等......

我的輸出如下:

204 jack_sparow 

http://testlink.com/test123&u_link=123_Doughboy® 

http://testlink.com/test346&u_link=348_༺༃ོེċℏυƿᾰċᾰ♭Իᾰ༂ི༻ 

等等。

由於某些原因,當輸入文件從第一行開始時,該行不會得到處理,並且不會出現在結果文件中。當第一行在輸入文件中留空時,輸出文件如上所示。將它移動到輸入文件中的下一行,在輸出文件中沒有區別。這是我的第一個問題。第二個是,我不知道如何分割輸入文件的行號和名稱,然後將數字移到行的前面,並命名到輸出文件中的鏈接後面,

我的代碼如下所示:

for line in open('test2.txt'): #reading file 

rec = line.strip() 

rec = rec.replace(" ", "_") #Need whitespaces and brackets removed from link so i replaced them with low line 
rec = rec.replace("(", "_") 
rec = rec.replace(")", "_") 

level = ('1', '2', '3', '4', '5', '6', '7', '8', '9') #line with number and name always starts with number 

link = ('h')   #line with link always starts with letter h as in http:// 

name = (rec[3:])  

if rec.startswith(link): 

    f = open("test5.txt","a") 

f.write(rec + "&u_link=")  #writes link and append $u_link= to the end of the line and this is the place where i want to append the name 

if rec.startswith(level) : 

    f = open("test5.txt","a") 

    f.write(rec + "\n\n")  # this is where i write name and number 

我知道代碼遠非完美,但我剛開始我的編程冒險,這是我第二次嘗試完成相同的任務。在我的raw_input嘗試失敗之後,我決定使用讀/寫文件方法,原因是名稱中包含的符號和花哨字體無法在Windows命令行中處理,但在Linux控制檯上運行良好(Windows中的cmd使用的編碼方式與utf不同-8)。

這是我第一次嘗試代碼工作得很好,但中繼手工輸入,而不是文件:

print "level?",  
level = raw_input()  # file should be sorted by this variable 
print "link?", 
link = raw_input()  
print "name?",   # Problem with fonts and symbols 
name = raw_input() 
name = name.replace(" ", "") #This removes spaces from the name as URL   cant have spaces 
ul = "&u_link="  #This have to be appended to the link followed by the name 
el = "\n"    #Empty line to separate links in test.txt file 
f = open("test.txt","a") 
f.write(el+level+" -- "+link+ul+name+el) #file writing 
print level+" -- "+link+ul+name   #printing in the console just to see if works 

我希望這解釋了什麼是我想要做的事。所有的幫助和建議非常感謝。請原諒我的任何錯誤。英語不是我的第一語言。

回答

0

所以我注意到,如果我使用reverse()反轉文件,它可以修復我的問題。由於某些原因,無論txt文件格式如何,python都會首先閱讀「鏈接」。 經過一小段研究,我發現完成任務的另一種方式是使用字符串列表並工作,而不考慮txt文件格式,這意味着它適用於鏈接位於包含數據或其上的行的實例。

這裏是我用來完成任務使用逆轉()的代碼:

import os 
import glob 

for line in reversed(open("test2.txt").readlines()): 
    rec = line.strip() 
    rec = rec.replace("<", "_") 
    rec = rec.replace(">", "_") 
    rec = rec.replace("&", "n") 
    rec = rec.replace(" ", "_") 
    rec = rec.replace("(", "_") 
    rec = rec.replace(")", "_") 
    rec = rec.replace('"', "_") 
    rec = rec.replace("'", "_") 
    level = ('1', '2', '3', '4', '5', '6', '7', '8', '9') 
    link = ('h') 
    if rec.startswith(link): 
    f = open("temp.txt","a") 
    f.write(rec + "&u_link=") 
    elif rec.startswith(level) : 
    f = open("temp.txt","a") 
    f.write(rec + "\n\n") 
    f.close() 
for line in reversed(open("temp.txt").readlines()): 
    lines = line.strip()  
    f = open("hitlistlinks.txt","a") 
    f.write(lines + "\n")  

files = glob.glob('temp.txt') 
for f in files: 
    os.remove(f) 

請注意,我在過程中,我和刪除創建的臨時文件:在結束

files = glob.glob('temp.txt') 
for f in files: 
    os.remove(f) 

我碼。爲了這種方式工作,我不得不導入os和glob方法。

現在我對解決方案並不完全滿意,所以我做了更多的研究。 最後,我寫了另一個代碼,從http://www.reddit.com/r/learnprogramming/ 一些幫助強烈推薦從Learnprogrammin @reddit的傢伙。得到幾乎即時的幫助和很多好的建議,所以如果你是一個相當新的編程,這是一個很好的地方檢查,如果你堆疊東西。他們也有freenode #Learnprogramming非常活躍的IRC頻道。

這是最後的代碼,更清潔,做這項工作:

# Open the file 
with open("test3.txt", "r") as f: 

# Here we're going to clean up the input file to wipe out 
# any whitespace at the beginning or end of each line 
    cleaned_lines = [] 
    for line in f: 
     cleaned_lines.append(line.strip()) 

# Now we'll recombine it back into a single string of text 
# with the lines separated by the \n character 
    all_text = "\n".join(cleaned_lines) 

# Split the text on blank lines. Groups should now be a list 
# of strings, where each group contains two adjacent lines 
# that contain a link and a strip of data 
    groups = all_text.split("\n\n") 

# Now we'll go through each group and break it apart into the 
# two separate lines. One of them will start with an "http" 
# and that one will be our link. 

    for group in groups: 

     line1, line2 = [x for x in group.split("\n") if x] 
     if line1.startswith("http"): 
      link = line1 
      rec = line2 
     elif line2.startswith("http"): 
      link = line2 
      rec = line1 
     else: 
     # If one of the two lines doesn't start with "http" we 
     # have a group that doesn't have a link. 
     # I'll just throw 
     # an error and bring the program to a halt. 
      raise Exception("This group is missing a link! format(group)) 

     # At this point the link variable contains the link, and 
     # the data variable contains the other line. Now we can process the input file as intended 
     # and it will work on either file. 
     rec = rec.replace("<", "_") 
     rec = rec.replace(">", "_") 
     rec = rec.replace("&", "n") 
     rec = rec.replace(" ", "_") 
     rec = rec.replace("(", "_") 
     rec = rec.replace(")", "_") 
     rec = rec.replace('"', "_") 
     rec = rec.replace("'", "_") 
     f = open("hitlist.txt","a") 
     f.write(link + "&u_link=" + rec + "\n\n") 
     f.close() 

我希望這將幫助其他有類似的問題,並告訴他們兩種不同的方法同樣的問題。僅供參考有兩個以上。