文本对齐问题:使用交替对齐聊天

2024-09-29 19:38:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试对下面的数组进行文本对齐,并将其转换为聊天系统,如图所示。到目前为止,我能够得到正确的对齐方式,但我一直在努力使它看起来像一个聊天对话

“1”、“2”表示用户及其消息,数组应确定对齐的交替顺序。宽度是一行的总宽度,用户宽度是用户可以在一行中使用的最大宽度。用户1向左对齐,用户2向右对齐

到目前为止,我有以下代码:

messages = [["1", "Bob hello"], ["2", "Alice hi"], ["1", "How is your life"], ["1", "Better than before"],
            ["2", "M super"], ["1", "Wow pro"]]
userWidth = 6
width = 15


def chat(messages, userWidth, width):
    user1 = []
    user2 = []
    sep_num = []
    order = []
    for message in messages:
        user = message[0]
        convo = message[1]
        windowStart = 0
        sep_num.append(len(convo) // userWidth + 1)
        order.append(user)
        for windowEnd in range(len(convo)):
            if windowEnd > 0:
                if (windowEnd + 1) % userWidth == 0:
                    if user == "1":
                        left_aligned = convo[windowStart:windowEnd + 1]
                        user1.append(left_aligned)
                    else:
                        right_aligned = convo[windowStart:windowEnd + 1]
                        user2.append(right_aligned)
                    windowStart = windowEnd + 1
                if windowEnd == len(convo) - 1:
                    if user == "1":
                        left_aligned = convo[windowStart:windowEnd + 1]
                        if len(left_aligned) == 1 and user1[-1][-3] != " ":
                            left_aligned = "".join([user1[-1][-1],left_aligned])
                            user1[-1] = user1[-1][:-1]
                        if len(left_aligned) == 1 and user1[-1][-3] == " ":
                            left_aligned = "".join([user1[-1][-3:], left_aligned])
                            user1[-1] = user1[-1][:-3]
                        user1.append(left_aligned)
                    else:
                        right_aligned = convo[windowStart:windowEnd + 1]
                        if len(right_aligned) == 1 and user2[-1][-3] != " ":
                            right_aligned = "".join([user2[-1][-1], right_aligned])
                            user2[-1] = user2[-1][:-1]
                        if len(right_aligned) == 1 and user1[-1][-3] == " ":
                            right_aligned = "".join([user1[-1][-3:], right_aligned])
                            user1[-1] = user1[-1][:-3]
                        user2.append(right_aligned)

    constructor(user1, user2, width, order, sep_num)

def constructor(user1, user2, width, order, sep_num):
    for i in range(len(user1)):
        if (len(user1[i])) > 1:
            if user1[i][0] == " ":
                user1[i] = user1[i][1:]
            space = width - len(user1[i])
            line = "|" + user1[i] + (" " * space)
            print(line)
    for i in range(len(user2)):
        if (len(user2[i])) > 1:
            if user2[i][-1] == " ":
                user2[i] = user2[i][:-1]
            space = width - len(user2[i])
            line = (" " * space) + user2[i] + "|"
            print(line)

这使得它看起来像这样:

|Bob he         
|llo            
|How is         
|your           
|life           
|Better         
|than           
|before         
|Wow            
|pro            
          Alice|
             hi|
          M sup|
             er|

但我如何将其转换为以下内容:

text_justification


Tags: 用户rightlenif宽度widthleftappend
1条回答
网友
1楼 · 发布于 2024-09-29 19:38:06

您可以尝试以下方法:

from textwrap import wrap

messages = [["1", "Bob hello"], ["2", "Alice hi"], ["1", "How is your life"], ["1", "Better than before"],
            ["2", "M super"], ["1", "Wow pro"]]

win = 15
lw = 6

print("+" + "*" * win + "+")
for num, msg in messages:
    pad = "<" if num == "1" else ">"
    print("\n".join(f"|{s:{pad}{win}}|" for s in wrap(msg, lw)))
print("+" + "*" * win + "+")

它给出:

+***************+
|Bob            |
|hello          |
|          Alice|
|             hi|
|How is         |
|your           |
|life           |
|Better         |
|than           |
|before         |
|              M|
|          super|
|Wow            |
|pro            |
+***************+

相关问题 更多 >

    热门问题