[Python] How to to parse html data table with characters and x-y coordinates and render them...

Discussão em 'Python' iniciado por Stack, Maio 22, 2025.

  1. Stack

    Stack Membro Participativo

    Here's the problem statement: use a link to a google doc and print the symbols per the x and y coordinates provided in the document. There are two google doc links provided in the assesment:

    This is an "F":

    https://docs.google.com/document/d/...C7lQiZm-CSEznKfN_HYNSpoXcZIV3Y_O3YoUB1ecq/pub

    This is a lengthier secret message:

    https://docs.google.com/document/d/...TvTFQI53erafhUkGot24ulET8ZRqFSzYoi3pLTGwM/pub

    I found two codes that work to do this with similar success - they both seem to handle the lengthier message well, but both have a printing error when it comes to plotting the "F". You can see both codes in this reddit post:

    https://www.reddit.com/r/learnpython/comments/1evo1zn/im_feeling_defeated/

    I ran the code that is easier to understand and copying its output below:

    • note: you may have to run it in a codespace and type "pip install html5lib" in your terminal in response to the initial error message.
    • As you can see its plotting the "F" inverted - so I'm trying to understand where I'm going wrong? I'm also not fluent in fully understanding all the loops here.

    Here is my script:

    import pandas as pd
    from bs4 import BeautifulSoup

    def getTableDataSorted(url):
    tableData = pd.read_html(url, header=0, flavor='bs4')
    return tableData[0].sort_values(by=["y-coordinate","x-coordinate"], ignore_index=True)


    def printData(tableData):
    xcoord = tableData['x-coordinate']
    ycoord = tableData['y-coordinate']
    char = tableData['Character']

    for i in range(0, len(ycoord)):
    if (i != 0) and (xcoord - xcoord[i - 1] != 1):
    # empty spaces
    print(" " * int((xcoord) - (xcoord[i - 1]) - 1), end='')
    if (i !=0) and (ycoord != (ycoord[i - 1])):
    # new line
    print('\r')
    print (char, end='')
    print('\n')

    tableData = getTableDataSorted("https://docs.google.com/document/d/e/2PACX-1vRMx5YQlZNa3ra8dYYxmv-QIQ3YJe8tbI3kqcuC7lQiZm-CSEznKfN_HYNSpoXcZIV3Y_O3YoUB1ecq/pub")
    printData(tableData)
    print(tableData)



    Here's the terminal output:

    █
    █▀▀
    █▀▀▀


    x-coordinate Character y-coordinate
    0 0 █ 0
    1 0 █ 1
    2 1 ▀ 1
    3 2 ▀ 1
    4 0 █ 2
    5 1 ▀ 2
    6 2 ▀ 2
    7 3 ▀ 2


    The expected result was:

    █▀▀▀
    █▀▀
    █


    Instead I'm getting this:

    █
    █▀▀
    █▀▀▀

    Continue reading...

Compartilhe esta Página