0

I'm attempting to load a CSV which looks like the following and create a tuple out of it, so that I can later access account1, account2, etc in code:

user,password
[email protected],password
[email protected],password2

So far, all I've been able to do is import the CSV.

import csv

# our csv file name
filename = "resources/accounts.csv"

with open(filename) as infile:
    reader = csv.reader(infile)

I'm looking for something like so: {("[email protected]", "password"), ("[email protected]", "password")}

Could someone advise on what to do from here?

4
  • Marcel: Just append each row (which will be a tuple of values) to a list. Commented Jan 25, 2020 at 1:48
  • @martineau Thanks. I was interested in turning them into a tuple instead of list due to the fact that it's immutable. Am I misunderstanding a fundamental concept here? Commented Jan 25, 2020 at 2:01
  • 1
    I don't think the immutability of a tuple is that useful in a useful like this. Who would try to change it, by mistake or evil intent? Commented Jan 25, 2020 at 5:59
  • Marcel: You could create a tuple-of-tuples — i.e. (('[email protected]', 'password'), ('[email protected]', 'password')) — with: tuple_of_tuples = tuple(row for row in csv.reader(infile)). Commented Jan 25, 2020 at 8:00

1 Answer 1

0
import csv # our csv file name 
filename = "resources/accounts.csv" 
with open(filename) as infile:
  reader = csv.reader(infile)

accounts = {"accounts" : []} 
for line in reader:
    accounts["accounts"].append({"user":line[0], "pass": line[1]})

print(accounts["accounts"][1]["user"])

Sorry I did this on mobile hope it shows up

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.