[英]how to read lines in python and then assign each line to a variable
I am trying to write a code that will take a file see those letters then take them and make them in to other letters.我正在尝试编写一个代码,该代码将获取一个文件,查看这些字母,然后将它们放入其他字母中。 hear is my code.
听到是我的代码。
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()
the output is output 是
would you like to type the message or read a file that has the message?: read
>>>
ideas?想法?
You can get a list of lines with readlines()
:您可以使用
readlines()
获取行列表:
e = open("file.txt", 'r')
lines = e.readlines()
e.close()
You can also iterate through the lines of a file with a for loop:您还可以使用 for 循环遍历文件的行:
e = open("file.txt", 'r')
for line in e:
#your code here
e.close()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.