- 帖子
- 37
- 精华
- 0
- 积分
- 476
- 阅读权限
- 30
- 注册时间
- 2018-3-31
- 最后登录
- 2019-10-26
|
本帖最后由 风扇很响 于 2018-4-1 18:10 编辑
"""
把一段字符串用“右起竖排”的古文格式输出,并且拿竖线符号作为每一列的分割符。
比如这段文字:
"静夜思 李白床前明月光,疑似地上霜。举头望明月,低头思故乡。"
输出结果:
低┊举┊疑┊床┊静
头┊头┊似┊前┊夜
思┊望┊地┊明┊思
故┊明┊上┊月┊
乡┊月┊霜┊光┊李
。┊,┊。┊,┊白
"""
# 从五言绝句开始
# 分析格式:标题+空格+作者+正文,五言绝句正文(包括标点)必然是6 * 4 = 24 个字符
def poetprint_5jue(poetryinput):
zishu = 5 + 1 # 每句诗的字数,加上一个标点,共6个
jushu = 4 # 五言绝句,共4句
title = poetryinput.split(" ", 1)[0] # 获取标题
poet = poetryinput.split(" ", 1)[1][0: len(poetryinput) - len(title) - 1 - zishu * jushu ] # 获取作者
zhenwen = []
for i in range (0, jushu): # 获取每句诗的内容
a = poetryinput[(len(title) + len(poet) + i * zishu + 1) : (len(title) + len(poet) + (i + 1) * zishu + 1)]
zhenwen.append(a)
# 防止每句字数和(标题+作者)的字数不一致导致打印问题,预先做调整
# 相等就不做任何操作
if (len(title) + len(poet) + 1) > zishu:# 正文字数短,正文补充空格
d = (len(title) + len(poet) + 1) - zishu
for i in range (0, jushu):
zhenwen= zhenwen+(" " * d)
if (len(title) + len(poet) + 1) < zishu:# 正文字数长,作者补充空格
d = zishu - (len(title) + len(poet) + 1)
poet = poet+(" " * d)
t_p = title+" "+poet# 合并标题和作者
# 打印正文
for j in range (0, len(t_p)):
for i in range (jushu - 1 + 1, 0, -1):
if zhenwen[i - 1][j] == " ":
print (" ", end = "") #一个空格不够宽度,再打一个凑足
print (zhenwen[i - 1][j] + "┊", end = "")
print (t_p[j])
print("\n")
poetryinput1 = "静夜思 李白床前明月光,疑似地上霜。举头望明月,低头思故乡。"
poetryinput2 = "登鹳雀楼 王之涣白日依山尽,黄河入海流。欲穷千里目,更上一层楼。"
poetryinput3 = "相思 王维红豆生南国,春来发几枝。愿君多采撷,此物最相思。"
poetprint_5jue(poetryinput1)
poetprint_5jue(poetryinput2)
poetprint_5jue(poetryinput3)
Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:04:45) [MSC v.1900 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>>
================= RESTART: D:\Python36\code\crossin\文字竖排.py =================
低┊举┊疑┊床┊静
头┊头┊似┊前┊夜
思┊望┊地┊明┊思
故┊明┊上┊月┊
乡┊月┊霜┊光┊李
。┊,┊。┊,┊白
更┊欲┊黄┊白┊登
上┊穷┊河┊日┊鹳
一┊千┊入┊依┊雀
层┊里┊海┊山┊楼
楼┊目┊流┊尽┊
。┊,┊。┊,┊王
┊ ┊ ┊ ┊之
┊ ┊ ┊ ┊涣
此┊愿┊春┊红┊相
物┊君┊来┊豆┊思
最┊多┊发┊生┊
相┊采┊几┊南┊王
思┊撷┊枝┊国┊维
。┊,┊。┊,┊
|
|