-2

我正在尝试编写一个代码,该代码将获取一个文件,查看这些字母,然后将它们放入其他字母中。听到是我的代码。

def decode():
    dd=input("would you like to type the message or read a file that has the message?:")
    if dd in['read']:
        e=open("file.txt", 'r')
        for i in range(7):
            ff=e.read()
            if ff in['wen']:
                print("new")
             ...
        e.close()
decode()

输出是

would you like to type the message or read a file that has the message?: read

>>>

想法?

4

1 回答 1

0

您可以通过以下方式获取行列表readlines()

e = open("file.txt", 'r')
lines = e.readlines()
e.close()

您还可以使用 for 循环遍历文件的行:

e = open("file.txt", 'r')
for line in e:
     #your code here
e.close()
于 2021-01-03T17:04:30.047 回答