mercredi 20 mai 2020

Python: CSV delimiter failing randomly

I have created a script which a number of random passwords are generated (see below)

import string
import secrets
import datetime

now = datetime.datetime.now()
T = now.strftime('%Y_%m_d')

entities = ['AA','BB','CC','DD','EE','FF','GG','HH']

masterpass = ('MasterPass' + '_' + T + '.csv')

f= open(masterpass,"w+")

def random_secure_string(stringLength):
    secureStrMain = ''.join((secrets.choice(string.ascii_lowercase + string.ascii_uppercase + string.digits + ('!'+'?'+'"'+'('+')'+'$'+'%'+'@'+'#'+'/'+':'+';'+'['+']'+'#')) for i in range(stringLength)))
    return secureStrMain
def random_secure_string_lower(stringLength):
    secureStrLower = ''.join((secrets.choice(string.ascii_lowercase)) for i in range(stringLength))
    return secureStrLower
def random_secure_string_upper(stringLength):
    secureStrUpper = ''.join((secrets.choice(string.ascii_uppercase)) for i in range(stringLength))
    return secureStrUpper
def random_secure_string_digit(stringLength):
    secureStrDigit = ''.join((secrets.choice(string.digits)) for i in range(stringLength))
    return secureStrDigit
def random_secure_string_char(stringLength):
    secureStrChar = ''.join((secrets.choice('!'+'?'+'"'+'('+')'+'$'+'%'+'@'+'#'+'/'+':'+';'+'['+']'+'#')) for i in range(stringLength))
    return secureStrChar

for x in entities:
     f.write(x + ',' + random_secure_string(6) + random_secure_string_lower(1) + random_secure_string_upper(1) + random_secure_string_digit(1) + random_secure_string_char(1) + ',' + T + "\n")

f.close()

I use pandas to get the code to import a list, so normally it is for 200-250 entities, not just the 8 in the example. The issue comes every so often where it looks like the comma delimiter fails to be read (see row 6 of attached photo) delimiter fail

In all the cases I have had of this (multiple run throughs), it looks like the 10th character is a comma, the 4 before (characters 6-9) are as stated in the script, but then instead of generating 6 initial characters (from random_secure_string(6)), it is generating 5. Could this be causing the issue? If so, how do I fix this? Thank you in advance




Aucun commentaire:

Enregistrer un commentaire