为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

ITMISS 2024-01-15 21:42:32
我有一个包含我的电子邮件和密码地址的文本文件,文本文件:电子邮件:email@address.com密码: 密码123我想使用变量“email”从文本文件中提取数据,并使用 Selenium 将其输入到我的代码中。例如:search = driver.find_element_by_name("emailAddress") search.send_keys(EmailFromTextFile)如何?
查看完整描述

3 回答

?
守着星空守着你

TA贡献1799条经验 获得超8个赞

使用:


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

contents = myfile.read()

print(contents)

myfile.close()

查看完整回答
反对 回复 2024-01-15
?
大话西游666

TA贡献1817条经验 获得超14个赞

这是使用正则表达式的方法:


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)


查看完整回答
反对 回复 2024-01-15
?
皈依舞

TA贡献1851条经验 获得超3个赞

尝试这个:


with open("file/location.txt","r") as f:

    txt = f.read()

email = txt.split("password")[0].split(":")[1].strip()

password = txt.split(":")[2].strip()


查看完整回答
反对 回复 2024-01-15
  • 3 回答
  • 0 关注
  • 31 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信