(第2坑)文字竖排

题目:把一段字符串用“右起竖排”的古文格式输出,并且拿竖线符号作为每一列的分割符。
比如这段文字:
"静夜思 李白床前明月光,疑似地上霜。举头望明月,低头思故乡。"
输出结果:
低┊举┊疑┊床┊静
头┊头┊似┊前┊夜
思┊望┊地┊明┊思
故┊明┊上┊月┊
乡┊月┊霜┊光┊李
。┊,┊。┊,┊白

思路:
1、切割空格符
2、切割句号
3、切割逗号,分离诗句和诗人,合并标题和诗人
4、补全空格
4、颠倒诗句顺序
5、将每句诗的字单独开来
6、展示

def change_pattern(poem):
    # 分离出 题目,诗人+诗句
    title,poet_n_poem  = poem.split()
    title = title.strip()
    poet_n_poem = poet_n_poem.strip()

    # 存放诗句的列表
    all_lines = []
    # 分离句号
    for one in poet_n_poem.split("。"):
        if ',' in one:
            # 分离逗号
            lines = one.split(",")
            # print(lines)
            # 判断是否是 诗人+诗句,且给每个诗句后面加逗号
            if len(lines[0]) != len(lines[1]):
                line_len = len(lines[1])
                poet = lines[0][:len(lines[0])-line_len]
                lines[0] = lines[0].strip(poet) + ","
                lines[1] = lines[1] + "。"
                line_len = line_len + 1
                all_lines = all_lines + lines
            else:
                lines[0] = lines[0] + ","
                lines[1] = lines[1] + "。"
                all_lines = all_lines + lines


    title_n_poet = title + " " + poet
    title_n_poet_len = len(title_n_poet)
    all_lines.insert(0,title_n_poet)
    all_lines.reverse()

    # 补全空格,使长度一致
    delta = line_len - title_n_poet_len
    # 诗句长度短
    if delta < 0:
        for i in range(0,len(all_lines)-1):
            all_lines[i] = all_lines[i] + abs(delta) * " "
    # 题目+空格+诗人 长度短
    elif delta > 0:
        all_lines[-1] = all_lines[-1] + abs(delta) * " "
    else:
        pass


    # 把诗句的每个字单独出来
    for i in range(0,len(all_lines)):
        all_lines[i] = list(all_lines[i])


    result_list = []
    j = 0
    while j < len(all_lines[0]):
        i = 0
        result_list.append([])
        # for i in range(0,len(all_lines)):
        #     result_list[j].append(all_lines[i][j])
        while i < len(all_lines):
            result_list[j].append(all_lines[i][j])
            i = i + 1
        j=j+1
    # print(result_list)

    for one in result_list:
        string = "|".join(one)
        print(string)



if __name__ == "__main__":
    poem = "锦瑟 李商隐锦瑟无端五十弦,一弦一柱思华年。庄生晓梦迷蝴蝶,望帝春心托杜鹃。沧海月明珠有泪,蓝田日暖玉生烟。此情可待成追忆," \
       "只是当时已惘然。"
    change_pattern(poem)

你可能感兴趣的:((第2坑)文字竖排)