2017-04-06 54 views
0

我正在寫一個類的代碼,並且在查找一行的中間時遇到了一些麻煩。我必須提示用戶輸入一個文件,讀取它,將它分成單獨的行,然後在每行的開始,中間和結尾添加一個特定的單詞。我不知道如何得到中間的東西如何在一個文件中找到一行的中間

file = str(input("Enter input file:" "")) 
my_file = open(file, "r") 
file_contents = my_file.read() 
#change all "e"s to "zw"s 
     for letter in file_contents: 
      if letter == "e": 
       file_contents = file_contents.replace(letter, "zw") 
#add "hokie" to beginning, middle, and end of each line 
     lines = file_contents.split('\n') 
#I know the following line is wrong, but I'm not sure how to fix it 
     middle_message = lines[len(lines) /2:] + "hokie" 
     message = "hokie" + middle_message + "hokie" 

回答

0

你可能會有更好的時間自己解決大部分問題。

這裏有一點提示:middle_message應該包含什麼? 對於一個具體的例子,如果行是'ABCD',我們想要將行更改爲'hokieABhokieCDhokie'。 看看您是否可以指出ABCD部件來自您的代碼片段。

相關問題