基于.txt文件的Python



我拥有一个包含许多行的txt文件。每一行都将作为下一个

email1:password1
email2:password2
...
...
...
emailxx:passwordxx

我想要一个每次读取文件行的python代码,下一个是打印

email=username1
pass=password1
email=username2
pass=password2
email=username3
pass=password3
...
...
...
email=usernamexx
pass=passwordxx

尝试以下操作:

with open('path/to/file.txt','r') as f:
values = f.readline().split(":")
print(f"email={values[0]}")
print(f"pass={values[1]}n")

谢谢我修复了

with open("xxx.txt") as f:
for line  in f.readlines():
#print(line)
values = line.split(":")
email = values[0]
pasw = values[1]
print(f"user={email}npass={pasw}n")

open函数为您提供了一个文件对象,并具有从文件中读取行的方法。

with open(filepath) as f:
for line  in f.readlines():
print(line)
info = line.strip(":"))
output = f"email={info[0]}npass={info[2]}"
print(output) 
f.close() #Close the file when you're done.

相关内容

  • 没有找到相关文章

最新更新