Python random digit letter and additional generator
Question: I would like to generate a random number string of N length with this code:

    
    
    import random
    import string
    
    N=512
    print ''.join(random.choice(string.ascii_uppercase + string.digits + string.ascii_lowercase) for x in range(N))
    

What is missing is that I would like to add "special characters like e.g.
"@,;:.§$%&/(!"" And I would like to output that to .txt file with a newline
after for a example 10,15 signs.

Any help would be great.

Thanks for the time.


Answer: `string` module has a class for such special characters:

    
    
    >>> print string.punctuation
    !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
    

To save generated string to file use something like:

    
    
    N=512
    chars_per_line = 15
    
    s = ''.join(random.choice(string.digits + string.ascii_letters + string.punctuation) for i in xrange(N))
    
    fh = open('filename', 'w')
    try:
        fh.writelines([s[i:i+chars_per_line]
                       for i in range(0, N, chars_per_line)])
    finally:
        fh.close()
    

