首页 > 解决方案 > 如何从文本文件中获取数据,然后在 Python 文件中使用提取的数据?

问题描述

我有一个包含我的电子邮件和密码地址的文本文件,

文本文件:

电子邮件:email@address.com

密码:password123

我想使用变量“email”从文本文件中提取数据,并使用 Selenium 将其输入到我的代码中。

例如:

search = driver.find_element_by_name("emailAddress")
search.send_keys(EmailFromTextFile)

如何?

标签: pythonseleniumtext-files

解决方案


利用:

myfile = open("text.txt","r")  # text.txt is the file name, r means read file.
contents = myfile.read()
print(contents)
myfile.close()

您可以在Python File Open中获得有关 Python 文件处理的更多信息。


推荐阅读