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
johndoe@gmail.com,password
janesmith@gmail.com,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: {("johndoe@gmail.com", "password"), ("janesmith@gmail.com", "password")}

Could someone advise on what to do from here?

Marcel Doe
  • 57
  • 7
  • Marcel: Just append each row (which will be a tuple of values) to a list. – martineau Jan 25 '20 at 01: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? – Marcel Doe Jan 25 '20 at 02: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? – hpaulj Jan 25 '20 at 05:59
  • Marcel: You could create a tuple-of-tuples — i.e. `(('johndoe@gmail.com', 'password'), ('janesmith@gmail.com', 'password'))` — with: `tuple_of_tuples = tuple(row for row in csv.reader(infile))`. – martineau Jan 25 '20 at 08:00

1 Answers1

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

jlewkovich
  • 2,725
  • 2
  • 35
  • 49
Krowvin
  • 86
  • 1
  • 6