简体   繁体   中英

How do I go about reading a csv file while in a loop

Hey so Im trying to make it so my code basically reads through a csv file like this.

gmail,password
Jamesz73an6@test.com,James9123
Ryan@test.com,Ryan9123

I want to try to make it so when it reads the first line in the loop, when it is ran again (if the user decides to run 2 tasks) it will go to the next line. My current code is like this.

taskCount = int(input('How many accounts would you like to warm? '))
    for i in range(taskCount):
            with open ('nikeaccounts.csv') as csvfile:
                reader = csv.DictReader(csvfile)
                for row[taskCount] in reader:
                    print('')
            login = (row['gmail'])
            password = (row['password'])

The code below prints the login and the password for each extracted line, but you can just as easily save them in a list for future use:

import csv
taskCount = int(input('How many accounts would you like to warm? '))
with open('contact_list.csv', 'r') as data:
    reader = list(csv.reader(data))
    for i in range(1, taskCount+1):
        print(f'login: {reader[i][0]}, password: {reader[i][1]}')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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