SSJX.CO.UK
Content

Generating simple passwords in a CSV file using Python

This program will generate and put a password into a CSV file if it detects a field called 'Password' in the header. The example below will explain it better! Note that this only generates simple random passwords and should not be used for anything serious as is!

Python Source

import random

# The value passed is the password length
def passgen(c):
    # Removed vowels and similar looking characters!
    alpha="bcdfghjkpqrstvwxyz23456789"
    pw=""
    for i in range(0,c):
        pos=random.randrange(0,len(alpha))
        pw+=alpha[pos]
        
    return pw

# Open our csv file
try:
    myfile=open("data.csv")
except:
    print("Could not open the file...")
    exit()

# Read in the row and replace or fill the password field
skip=True
found=False
pwpos=0
    
for line in myfile:
    fields=line.split(",")
    # Not skipping this time, finding Password field position first! 
    if skip==True:
        for f in fields:
            if f.strip()=="Password":
                print(line.strip())
                found=True
                break
            pwpos+=1
        
        if found==False:
            print("Password field not found?")
            break
       
        skip=False
        continue

    fields[pwpos]=passgen(6)
    
    i=0
    for f in fields:
        print(f.strip(),end="")
        if i!=len(fields)-1:
            print(",",end="")
        i+=1
    print()
    
myfile.close()

Running the program

Save the above as something, passgen.py for example, then run it specifying a CSV file. The example file looks like:

First,Last,Password,Eyes#skip
fred,bloggs,,blue
joe,bloggs,,green

Running the program against the above generates:

#skip>py passgen.py data.csv
First,Last,Password,Eyes
fred,bloggs,q4gt97,blue
joe,bloggs,7w78f4,green

To create a new file with these passwords, redirect the output by running as 'py passgen.py data.csv >new.csv' or similar.

Created 23/07/2025