Python文件.write(电子邮件+'\n')名称错误:名称'电子邮件'未定义



我从我的代码中得到了这个错误,我不明白为什么它不工作

with open("emails.txt",'r') as file:
for line in file:
grade_data = line.strip().split(':')
email = grade_data[0]
password = grade_data[1]
with open("emails_sorted.txt",'a') as file:
print(Fore.YELLOW + "Sorting email...")
file.write(email + 'n')
with open("passwords.txt",'a') as file:
print(Fore.YELLOW + "Sorting password...")
passwordspecial = password + '!'
file.write(passwordspecial + 'n')
print(Fore.GREEN + "Done!")

在迭代完其他文本文件的内容后,您将打开emails_sorted.txt。您可以通过保存从emails.txt读取的数据并在打开emails_sorted.txtpasswords.txt:时再次迭代来解决此问题

emails = []
passwords = []
with open("emails.txt", "r", encoding="utf-8") as file:
for line in file.readlines():
grade_data = line.strip().split(":")
emails.append(grade_data[0])
passwords.append(grade_data[1])
with open("emails_sorted.txt", "a", encoding="utf-8") as file:
print(Fore.YELLOW + "Sorting email...")
for email in emails:
file.write(email + "n")
with open("passwords.txt", "a", encoding="utf-8") as file:
print(Fore.YELLOW + "Sorting password...")
for password in passwords:
passwordspecial = password + "!"
file.write(passwordspecial + "n")
print(Fore.GREEN + "Done!")

在所示的代码for line in file中,如果文件为空,则不会运行循环,因此不会定义电子邮件。如果文件为空,请尝试停止进程,或者为电子邮件设置默认值

最新更新