因此,我基本上有一个account.txt文件,其中包含我创建的帐户,如下所示:
Richard:Milo:RichardMilo@email.info:0495612970
Adrian:Clark:ClarkAdrian283406@email.info:0490714484
Michael:Robinson:13Mich@email.info:0446088017
Julio:Lucas:JulioLucas60@email.info:0454710033
如您所见,它们的格式为FirstName:LastName:Email:Phone
简单地说,我希望我的python脚本要做的是读取每一行,并在每行中返回名,姓,电子邮件和电话作为“电子邮件”,“ first_name” ,“ last_name”和“ phone”变量,然后完全删除该行,然后移至下一行,执行完全相同的操作,然后删除该行,依此类推。等等。拍拍只是返回每行的一部分,这给了我麻烦。
这是我当前的python代码:
submit_count = input('\nEnter Desired Number of Entries: ')
chrome_options = webdriver.ChromeOptions()
driver = webdriver.Chrome(chrome_options=chrome_options)
def enterraffle():
email = #NOT SURE HOW TO GET THE EMAIL FROM ACCOUNTS.TXT HERE
first_name = #NOT SURE HOW TO GET THE FIRST NAME FROM ACCOUNTS.TXT HERE
last_name = #NOT SURE HOW TO GET THE LAST NAME FROM ACCOUNTS.TXT HERE
phone = #NOT SURE HOW TO GET THE PHONE FROM ACCOUNTS.TXT HERE
driver.get(‘Login URL’)
driver.find_element_by_xpath('//*[@id="email"]').send_keys(email)
driver.find_element_by_xpath('//*[@id="password"]').send_keys(‘password123’)
print('{}/{} Successful Raffle Entries ({} {}:{})'.format(i, submit_count, first_name, last_name, email))
with open("entries_nl.txt", "a") as f:
f.write(first_name + ":" + last_name + ":" + email + ":" + phone + ":" + "\n")
f.close()
time.sleep(2)
#DELETE THAT LINE FROM ACCOUNTS.TXT HERE
driver.delete_all_cookies()
for i in range(int(submit_count)):
enterraffle()
感谢您的帮助!
答案 0 :(得分:0)
lines = []
fields = ['first_name', 'last_name', 'email', 'phone']
with open('accounts.txt') as fp:
for line in fp:
values = [x.strip() for x in line.split(':')]
lines.append(dict(zip(fields, values)))
答案 1 :(得分:0)
您可以使用csv.reader模块来读取csv文件。
import csv
#Open the file
lines = []
with open('accounts.txt') as fp:
#Create a csv reader on the file
reader = csv.reader(fp, delimiter=":")
#Iterate through each row
for row in reader:
lines.append(row)
print(lines)
输出将为
[
['Richard', 'Milo', 'RichardMilo@email.info', '0495612970'],
['Adrian', 'Clark', 'ClarkAdrian283406@email.info', '0490714484'], ['Michael', 'Robinson', '13Mich@email.info', '0446088017'],
['Julio', 'Lucas', 'JulioLucas60@email.info', '0454710033']
]
因此,第一封电子邮件为lines[0][2]
然后删除文件的内容,只需以w+
模式打开文件即可
#Open the file in w+ to delete all rows
filename = "accounts.txt"
f = open(filename, "w+")
f.close()
然后您可以将此输出用于enterraffle()
函数