如何在文件中找到一行的中间位置

时间:2017-04-06 00:20:03

标签: python-3.x caesar-cipher

我正在为课程编写代码,但在查找行中间时遇到了一些麻烦。我必须提示用户输入文件,读取它,将其拆分为单独的行,然后在每行的开头,中间和末尾添加一个特定的单词。我不知道如何获得中间

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"

1 个答案:

答案 0 :(得分:0)

你可能有更好的时间自己解决大部分问题。

这里有一个暗示:middle_message应包含哪些内容? 举一个具体的例子,如果该行是'ABCD',我们希望将该行更改为'hokieABhokieCDhokie'。 看看您是否可以在代码段中指出ABCD部分的来源。