我的代碼有幾個問題。我瞭解其中一個,但不知道如何解決它。代碼是爲了讀取文本文件。讀/寫文件:按特定順序寫入行 - 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
我希望這解釋了什麼是我想要做的事。所有的幫助和建議非常感謝。請原諒我的任何錯誤。英語不是我的第一語言。