亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何從文本文件中獲取數據,然后在 Python 文件中使用提取的數據?

如何從文本文件中獲取數據,然后在 Python 文件中使用提取的數據?

ITMISS 2024-01-15 21:42:32
我有一個包含我的電子郵件和密碼地址的文本文件,文本文件:電子郵件:[email protected]密碼: 密碼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 關注
  • 161 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號