Python Forum
Adding a 3rd key in a csv file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Adding a 3rd key in a csv file
#1
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.
Reply
#2
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.
Reply
#3
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  File path by adding various variables Mishal0488 2 1,025 Apr-28-2023, 07:17 PM
Last Post: deanhystad
  Adding to an XML file TeXaSpEtE83 0 1,266 Dec-22-2021, 08:28 AM
Last Post: TeXaSpEtE83
  How to rename a CSV file by adding MODIFIED in the filename? Python_User 25 8,098 Dec-13-2020, 12:35 PM
Last Post: Larz60+
  Adding markers to Folium map only adding last element. tantony 0 2,121 Oct-16-2019, 03:28 PM
Last Post: tantony

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020