鱼C论坛

 找回密码
 立即注册
查看: 2110|回复: 7

求助——文本框装饰

[复制链接]
发表于 2019-5-12 22:27:36 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
给出了一段文字,之后需要利用字符串和格式化输出的方式进行装饰,每行前面的空格要保留,而每行最后的空格应该去除,同时加上装饰框。
需要对decorate_text函数进行定义和格式化输出(我想?)
  1. #! /usr/bin/env python3
  2. #  -*- coding: utf-8 -*-

  3. def decorate_text(text):
  4.     '''
  5.     有一段英文文本(这样我们不用考虑中文文本导致的对齐问题),该文本有多行,每行前后可能会有空格,
  6.     每行前面的空格要保留,而每行最后的空格应该去除。请在这段英文文本上面加上装饰栏。
  7.     有一段英文文本(这样我们不用考虑中文文本导致的对齐问题),该文本有多行,每行前后可能会有空格,
  8.     每行前面的空格要保留,而每行最后的空格应该去除。请在这段英文文本上面加上装饰栏,如下所示
  9. +-------------------------------------------------------------------------------+
  10. |  The King and Queen of Hearts were seated on their throne when they arrived,  |
  11. |with a great crowd assembled about them--all sorts of little birds and beasts, |
  12. |as well as the whole pack of cards: the Knave was standing before them, in     |
  13. |chains, with a soldier on each side to guard him; and near the King was the    |
  14. |White Rabbit, with a trumpet in one hand, and a scroll of parchment in the     |
  15. |other. In the very middle of the court was a table, with a large dish of tarts |
  16. |upon it: they looked so good, that it made Alice quite hungry to look at them--|
  17. |`I wish they'd get the trial done,' she thought, `and hand round the           |
  18. |refreshments!'                                                                 |
  19. |                                                                               |
  20. |  But there seemed to be no chance of this, so she began looking at everything |
  21. |about her, to pass away the time.                                              |
  22. |                                                                               |
  23. +-------------------------------------------------------------------------------+
  24.     '''

  25. if __name__ == '__main__':
  26.     print('-'*40)
  27.     print('将输入的文本加上装饰条')

  28.    
  29.     text = r'''  The King and Queen of Hearts were seated on their throne when they arrived,      
  30. with a great crowd assembled about them--all sorts of little birds and beasts,   
  31. as well as the whole pack of cards: the Knave was standing before them, in  
  32. chains, with a soldier on each side to guard him; and near the King was the   
  33. White Rabbit, with a trumpet in one hand, and a scroll of parchment in the     
  34. other. In the very middle of the court was a table, with a large dish of tarts            
  35. upon it: they looked so good, that it made Alice quite hungry to look at them--   
  36. `I wish they'd get the trial done,' she thought, `and hand round the     
  37. refreshments!'

  38.   But there seemed to be no chance of this, so she began looking at everything           
  39. about her, to pass away the time.
  40.     '''

  41.     decorate_text(text)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-5-13 09:12:17 | 显示全部楼层
本帖最后由 wp231957 于 2019-5-13 09:23 编辑

大约我能做到这样了
  1. text = r'''  The King and Queen of Hearts were seated on their throne when they arrived,      
  2. with a great crowd assembled about them--all sorts of little birds and beasts,   
  3. as well as the whole pack of cards: the Knave was standing before them, in  
  4. chains, with a soldier on each side to guard him; and near the King was the   
  5. White Rabbit, with a trumpet in one hand, and a scroll of parchment in the     
  6. other. In the very middle of the court was a table, with a large dish of tarts            
  7. upon it: they looked so good, that it made Alice quite hungry to look at them--   
  8. `I wish they'd get the trial done,' she thought, `and hand round the     
  9. refreshments!'

  10.   But there seemed to be no chance of this, so she began looking at everything           
  11. about her, to pass away the time.
  12.     '''
  13.    
  14. lines=text.split("\n")  
  15. maxlen=0  
  16. for line in lines:
  17.   if len(line.rstrip())>maxlen:maxlen=len(line.rstrip())
  18. print("+","-"*(maxlen),"+")  
  19. for line in lines:
  20.   print("|","{0:<{1}s}".format(line.rstrip(),maxlen),"|")
  21. print("+","-"*(maxlen),"+")   
  22.    
  23.   
复制代码
批注 2019-05-13 092114.png
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-5-13 15:49:56 | 显示全部楼层
wp231957 发表于 2019-5-13 09:12
大约我能做到这样了

请问第18行到第20行是什么意思呢?没有看懂
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-5-13 15:52:31 | 显示全部楼层
ggg2000 发表于 2019-5-13 15:49
请问第18行到第20行是什么意思呢?没有看懂

17-18  :寻找最长子串
19 打印顶封装线
22打印底封装线

20-21  打印边封装线 及正文串
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-5-13 16:02:06 | 显示全部楼层
wp231957 发表于 2019-5-13 09:12
大约我能做到这样了

感谢您的代码!我把这个代码放入了decorate_text的函数里面 但是在段落上面会出现好多好多(10行?)的上端装饰线,请问这是为什么呢?如何解决呢?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-5-13 16:04:14 | 显示全部楼层
ggg2000 发表于 2019-5-13 16:02
感谢您的代码!我把这个代码放入了decorate_text的函数里面 但是在段落上面会出现好多好多(10行?)的上 ...

把全部代码都发出来,我才好判断是哪里出了问题啊
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-5-14 08:01:52 | 显示全部楼层
wp231957 发表于 2019-5-13 16:04
把全部代码都发出来,我才好判断是哪里出了问题啊

我发的那个就是全部代码啦!已经解决 在循环结构那里提前一下就可以变成单个装饰栏了!谢谢您!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-5-14 09:49:57 | 显示全部楼层
ggg2000 发表于 2019-5-14 08:01
我发的那个就是全部代码啦!已经解决 在循环结构那里提前一下就可以变成单个装饰栏了!谢谢您!

你应该结帖,再这个坛子里 叫设定最佳答案
否则 帖子状态显示 求助  还会有人继续回复的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-1-15 18:47

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表