Python Forum

Full Version: Adding a 3rd key in a csv file
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

The following code adds the ssid and pass in a dat file as "ssid";"pass".

ssid = "test"
password = "pass"
profiles = {}
profiles[ssid] = password

    def write_profiles(profiles):
    lines = []
    for ssid, password in profiles.items():
        lines.append("%s;%s\n" % (ssid, password))
    with open("text.dat", "w") as f:
        f.write(''.join(lines))
I'm trying to add a third key to the file but I just cannot get the syntax correct.

ssid = "test"
password = "pass"
token = "shfg53rdf"
profiles = {}
profiles[ssid] = password
profiles[profiles] = token

def write_profiles(profiles):
    lines = []
    for ssid, password, token in profiles.items():
        lines.append("%s;%s;%s\n" % (ssid, password, token))
    with open("text.dat", "w") as f:
        f.write(''.join(lines))

#should save keys: "test";"pass";"shfg53rdf"
I appreciate any help.
ssid = "test"
password = "pass"
token = "shfg53rdf"
profiles = {}
profiles[ssid] = (password, token)
#profiles[profiles] = token
 
def write_profiles(profiles):
    lines = []
    for ssid, _ in profiles.items():
        password = _[0]
        token = _[1]
        lines.append("%s;%s;%s\n" % (ssid, password, token))
    with open("text.dat", "w") as f:
        f.write(''.join(lines))
NOTE: storing raw passwords in a file is a bad practice; Each password should be hashed (and salted)
before it is being stored somewhere.
(Mar-01-2019, 01:32 AM)scidam Wrote: [ -> ]NOTE: storing raw passwords in a file is a bad practice;
Thank you. That's my next step.