简体   繁体   English

For循环python x文件中的次数

[英]For Loop in python x number of times from a file

The script runs for the number of accounts that are present in the accounts.txt file. 该脚本将根据accounts.txt文件中存在的帐户数运行。 I want to run the script x number of times, no matter how many accounts are present in the accounts.txt file. 我想运行脚本x次,无论account.txt文件中存在多少个帐户。 So I just enter an input 10 and the script should run the for loop for only 10 times. 所以我只输入10,脚本应该只运行10次for循环。 Below is my code. 下面是我的代码。

Can someone please help me as to how to fix the for loop or add a new parent for the for loop? 有人可以帮我解决如何修复for循环或为for循环添加新的父级吗?

file = open('accounts.txt','r')

for line in file:
    credentials = line.split(";")
    username = credentials[0]
    password = credentials[1]
    comment = credentials[2]

    chromedriver = "/Users/Ali/Downloads/chromedriver"

    os.environ["webdriver.chrome.driver"] = chromedriver
   # driver = webdriver.Chrome(chromedriver)
    chrome_options = webdriver.ChromeOptions()
    chrome_options.add_argument("--mute-audio")

this should work: 这应该工作:

for count, line in enumerate(file):
    credentials = line.split(";")
    username = credentials[0]
    password = credentials[1]
    comment = credentials[2]

    chromedriver = "/Users/Ali/Downloads/chromedriver"

    os.environ["webdriver.chrome.driver"] = chromedriver
   # driver = webdriver.Chrome(chromedriver)
    chrome_options = webdriver.ChromeOptions()
    chrome_options.add_argument("--mute-audio")

    if count == 9: # count starts at 0
        break

Put the reading thing in a function : 将阅读的东西放在一个函数中:

def run_loop():
    file_han = open('accounts.txt','r')
    file = file_han.read().split('/n') #you need to read and split the file w.r.t lines
    file_han.close()
    for line in file:
        credentials = line.split(";")
        username = credentials[0]
        password = credentials[1]
        comment = credentials[2]

        chromedriver = "/Users/Ali/Downloads/chromedriver"

        os.environ["webdriver.chrome.driver"] = chromedriver
        # driver = webdriver.Chrome(chromedriver)
        chrome_options = webdriver.ChromeOptions()
        chrome_options.add_argument("--mute-audio")

#Now loop the function over and over
i=0
while i =! input('Enter iterations no:'):
    run_loop()
    i=i+1

Another way to solve this problem is to use itertools.islice() which allows you to pick exactly as many values from an iterator (eg a file pointer) that you want: 解决此问题的另一种方法是使用itertools.islice() ,它使您可以从迭代器(例如文件指针)中选择所需数量的值:

import itertools

with open('accounts.txt', 'r') as file:
    wanted_lines = 10
    for line in itertools.islice(file, wanted_lines):
        credentials = line.split(";")
        username = credentials[0]
        password = credentials[1]
        comment = credentials[2]

        chromedriver = "/Users/Ali/Downloads/chromedriver"

        os.environ["webdriver.chrome.driver"] = chromedriver
       # driver = webdriver.Chrome(chromedriver)
        chrome_options = webdriver.ChromeOptions()
        chrome_options.add_argument("--mute-audio")

Here, in addition, I have used the with open... construction to open the file to read. 另外,在这里,我使用了with open...构造来打开要读取的文件。 The benefit of doing this, is that the file will automatically close when reaching the end of the with block. 这样做的好处是,文件将在到达with块的末尾时自动关闭。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM