#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2014
    Posts
    3
    Rep Power
    0

    ASCII word script


    Hi
    I'm trying to write a script to generate ASCII glyphs from a word. I'm a beginner learning python so how I've thought to do this is to split the multi-line string into a list (breaking at \n) and then join each letter's list together to form lines. This last part is what I'm having trouble with.
    If anyone could give me feedback. [Python] a = """ # ### ##### ### ### ### ### ########### ### - Pastebin.com please.

    Thanks,
    Greg
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,997
    Rep Power
    481
    Your characters have different numbers of lines. How do you want them aligned? Are you looking for what you've got (assuming I delete the program lines containing the undefined comp) with characters squished, or more like what figlet produces (long lines will be legible in figlet)?
    Code:
    $ python x.py 
    
        #########
       ############
      ########    ##
     ### ############
    ###   ############
    ##############    ##
    ###     ######    ###
    ###     #############
    ###     #############
     
    $ figlet AB
        _    ____  
       / \  | __ ) 
      / _ \ |  _ \ 
     / ___ \| |_) |
    /_/   \_\____/ 
                   
    $ # line left blank
    ps. pyfiglet is public domain which might interest you.
    Last edited by b49P23TIvg; December 9th, 2014 at 04:00 PM.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2014
    Posts
    3
    Rep Power
    0
    Hi,
    Ye I'm basically imitating programs like figlet so I do want spaces between characters not as I previously showed.

    [Python] # ASCII glyphs a = """ .....#..... ....###.... ...#####... ..### ###.. . - Pastebin.com

    I've slightly updated it changing the ASCII glyph's spacing (not to be as intended, just to work with) and at a point where I can print the first value from each list in the dictionary as a row. However it's only displaying characters 'letterrange' at the moment and I need to work in 'query' along with the spacing.

    I started to go through Dive into Python last night and thought about doing this, but taking alot longer than expected!

    Thanks,
    Greg
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,997
    Rep Power
    481
    locals()[string_value_of_variable_name] seems like a nice replacement for eval. Anyway, this is starting to work:
    Code:
    # ASCII glyphs
     
    a = """
    .....#.....
    ....###....
    ...#####...
    ..### ###..
    .###   ###.
    ###########
    ###     ###
    ###     ###
    ###     ###
    """
     
    b = """
    ########..
    #########.
    ###    ##.
    #########.
    #########.
    ###    ##.
    ###    ###
    ##########
    ##########
    """
     
    c = """
    ...#########
    ..#########.
    .####.......
    ####........
    ###.........
    ###.........
    .####.......
    ..#########.
    ...#########
    """
     
     
    # Define what characters there are
    letterrange = list("abc")
     
    letters = {}
     
    # (So don't have to manually put in a dictionary)
    # Dictionary where 'key' corresponds to the letter and 'value' the variable
    for i in letterrange:
        letters[i] = locals()[i]
        letters.update({i : letters[i].split('\n')})
     
    # The query that's being converted to ASCII glyphs
    query = list("bac") # not used yet
     
    GLYPHS = [letters[c] for c in query]
    
    for t in zip(*GLYPHS):
        print('|'.join(t))
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2014
    Posts
    3
    Rep Power
    0
    Brill', thanks for that.

    I think that'll do for now...

    Greg

    Code:
    #!/usr/bin/evn python3
    import sys
    
    # ASCII glyphs
    ascii_glyphs = dict(
    a = """
    .....#.....
    ....###....
    ...#####...
    ..### ###..
    .###   ###.
    ###########
    ###     ###
    ###     ###
    ###     ###
    """,
    
    b = """
    ########..
    #########.
    ###    ##.
    #########.
    #########.
    ###    ##.
    ###    ###
    ##########
    ##########
    """,
    
    c = """
    ...#########
    ..#########.
    .####.......
    ####........
    ###.........
    ###.........
    .####.......
    ..#########.
    ...#########
    """)
    
    # Define what characters there are
    letterrange = list("abc")
    
    # Split glyph into lines as a list in a dictionary
    for i in ascii_glyphs:
        ascii_glyphs.update({i: ascii_glyphs[i].split('\n')})
    
    # The query that's being converted to ASCII glyphs
    if len(sys.argv) < 2:
        sys.exit("\nMust enter a word to be converted\n")
    else:
        query = list(str(sys.argv[1]))
    
    GLYPHS = [ascii_glyphs[c] for c in query if c in letterrange]
    
    if len(GLYPHS) < 1:
        sys.exit("\nNot in letter range!\n")
    
    for t in zip(*GLYPHS):
        print ('   '.join(t))

IMN logo majestic logo threadwatch logo seochat tools logo