generating random passwords ... for a csv file with user details

Discussion in 'Python' started by k.i.n.g., May 28, 2006.

  1. k.i.n.g.

    k.i.n.g. Guest

    Hi,

    I have a csv file which in taken as the input file for adding users in
    my linux mail server with the format

    userid,fullname,passwword,dateofbith

    Now I have to write a script to generate random password in the
    password field for each user. A simple algorithm is sufficient for
    passwords

    I being new to scripting would seek all your help in doing so

    Thanks In advance

    kanthi
     
    k.i.n.g., May 28, 2006
    #1
    1. Advertisements

  2. import random

    def rand_str(len):
    chars = ''.join(['abcdefghijklmnopqrstuvwxyz',
    'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
    '1234567890',
    '_+']) # plus whatever additional characters you
    want
    return ''.join([random.choice(chars) for i in range(len)])

    print rand_str(10)
     
    andychambers2002, May 28, 2006
    #2
    1. Advertisements

  3. Kanthi skrev:
    #v-

    Mvh,
     
    Klaus Alexander Seistrup, May 28, 2006
    #3
  4. k.i.n.g. enlightened us with:
    Check out the source of pwsafe, it has a great password generator. It
    can generate with different lengths, based on amount of entropy, and
    can also generate different types (alpha/digit, hex, easy to read
    alpha/digit, etc.)

    Sybren
     
    Sybren Stuvel, May 28, 2006
    #4
  5. k.i.n.g.

    k.i.n.g. Guest

    Hi ALL,

    I am sorry for not mentioning that I am new to python and scripting.
    How can I add the above script to handle csv file. I want the script to
    generate passwords in the passwords column/row in a csv file.

    userid,realname,dateofB,passwd

    The script should read the userid and genrate the password for each
    user id (there are thousands of userids)

    Kanthi
     
    k.i.n.g., May 29, 2006
    #5
  6. k.i.n.g.

    Jon Clements Guest

    Something like:

    import csv
    in_csv=csv.reader( file('your INPUT filenamehere.csv') )
    out_csv=csv.writer( file('your OUPUT filenamehere.csv','wb') )
    ## If you have a header record on your input file, then
    out_csv.writerow( in_csv.next() )
    ## Iterate over your input file
    for row in in_csv:
    # Row will be a list where row[0]=userid and row[3]=passwd
    password=some_function_as_advised_by_rest_of_group()
    # Assuming you want to write password as new field then
    out_csv.writerow( row + [password] )
    # Assuming you want to over-write password field then
    row[3] = password
    out_csv.writerow(row)

    All the best,

    Jon.
     
    Jon Clements, May 29, 2006
    #6
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.