如何从文本文件中获取数据,然后在 Python 文件中使用提取的数据?

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

文本文件:

电子邮件:email@address.com

密码: 密码123

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

例如:

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

如何?


ITMISS
浏览 38回答 3
3回答

守着星空守着你

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

大话西游666

这是使用正则表达式的方法:import re# There would be other code here to set up Selenium and# define the variable `driver`with open('/tmp/data.txt') as f:    buf = f.read()expr = re.compile(r"email:\s*([^\s]+)\s+password: (.*)")m = expr.match(buf)if m:    email = m.group(1)    password = m.group(2)    search = driver.find_element_by_name("emailAddress")     search.send_keys(email)

皈依舞

尝试这个:with open("file/location.txt","r") as f:    txt = f.read()email = txt.split("password")[0].split(":")[1].strip()password = txt.split(":")[2].strip()
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python