0

My code successfully sends a slack message from python using this function/code I Omitted the details of the code that are not relevant:

def post_to_slack():
    (cursor initialized from query)
    heading = [item[0] for item in cursor.description]
    row1 = cursor.fetchone()
    col_heads = "%-10s %-51s %-52s %s" % (heading[0],heading[1],heading[2],heading[3])
    while row1:
        vSecond = row[1][:30]
        vThird = row[2][:30]
        row_form = "%-14s %-41s %-39s %s" % (row1[0], vSecond, vThird, row1[3])

        send_slack_messg(col_heads, webhook_url)
        send_slack_messg(row_form, webhook_url)
        row1 = cursor.fetchone()`

Again, the slack message is sent with no problems. The first row is formatted perfectly. The second row is similar to the first but slight shift in spacing in two or three of the column values. The 3rd row and beyond are significantly shifted by a random amount of spaces (not over 10 spaces but significant enough to not be a pleasant read.

Question: Is there a way to have my slack output be consistently formatted? Or is there a Python slack library that does this? Appreciate any help. Thx

I tried adjusting the format values of "%-Xs" - No success I tried using other formatting styles like "{} {} {} {}".format(...). Got worse or same results. Also tried enclosing with triple tics '''message''' still same issue.

Barmar
  • 741,623
  • 53
  • 500
  • 612
eagle17
  • 1
  • 2
  • Your formatting should be the same in every row. Do you see the same problem if you print the strings instead of sending them to slack? – Barmar May 04 '23 at 21:49
  • Slack uses proportional fonts, so columns won't line up nicely unless you send as a code block. – Barmar May 04 '23 at 21:49
  • Thanks for the response. When I use the same formatting numbers I used when it printed out OK (without slack) to send the same message to slack it did not come out OK in Slack. The spacing was all messed up. So then I tried to adjust the format numbers and I was able to get the first row to print OK but then the subsequent rows gradually lost the neat formatting. Ok, I see a link above that says this question has already been answered. I didn't see the table topic in the list that was presented to me to check when I created this question. Will check it out. Looks like what I need. Thx! – eagle17 May 05 '23 at 02:12

0 Answers0