python not readable_如何在python代码中使段落可读[重复](How to make paragraphs readable within python code [duplic...

如何在python代码中使段落可读[重复](How to make paragraphs readable within python code [duplicate])

我经常要向用户提供文本段落。 我对我向用户呈现文本的知识很满意(通常使用\n或文本换行使其在用户方面令人满意)但是有没有办法在我的实际代码中使用回车而没有\n

a = "this is a really long line of text that i need to show to the user. this is a really long line of text that i need to show to the user. this is a really long line of text that i need to show to the user. this is a really long line of text that i need to show to the user."

b = "this is a really long line of text that i need to show to the user.\n

this is a really long line of text that i need to show to the user.\n

this is a really long line of text that i need to show to the user.\n"

所以我的目标是在我的代码中将“a”分解为美学上令人愉悦的块,但仍然使用wordwrap将文本显示给用户。 我知道这样做的唯一方法是在'b'中显示,但这不适用于自动换行

This question already has an answer here:

I often have to present paragraphs of text to users. I'm ok with what I know about presenting the text to the user (usually using \n or text wrapping to make it pleasing on the user side) but is there a way to have carriage returns in my actual code without \n

a = "this is a really long line of text that i need to show to the user. this is a really long line of text that i need to show to the user. this is a really long line of text that i need to show to the user. this is a really long line of text that i need to show to the user."

b = "this is a really long line of text that i need to show to the user.\n

this is a really long line of text that i need to show to the user.\n

this is a really long line of text that i need to show to the user.\n"

So my goal is to break 'a' into aesthetically pleasing chunks in my code but still use wordwrap for displaying the text to the user. The only way I know to do that is shown in 'b' but that does not work with word wrapping

原文:https://stackoverflow.com/questions/25413674

更新时间:2020-01-21 09:52

最满意答案

所以你希望它在你的实际代码中可读吗? Python将隐式地连接代码中的相邻字符串。 要使它跨行工作,您可以使用行继续符\ ,或者,最好将其包装在括号中。

a = ("This is a really long\n"

"line of text that I\n"

"need to show to the user.")

print(a)

So you want it readable in your actual code? Python will implicitly concatenate adjacent strings in code. To get it to work across lines you can use the line continuation character \, or, preferably, wrap it in parentheses.

a = ("This is a really long\n"

"line of text that I\n"

"need to show to the user.")

print(a)

2014-08-20

相关问答

如果你不需要,不要import System.FilePath.Posix 。 System.FilePath根据您正在编译的平台导出System.FilePath.Posix或System.FilePath.Windows 。 你的words2实施是好的,但没有任何解释为什么它做到了。 这更加明显,效率差异不显着。 alphaWords = words . map (\c -> if isAlpha c then c else ' ')

searchWords改进: - wordsPer

...

通过打印转义码\033[0m ,您可以重置颜色: >>> print '\033[95m' + "This is a purple output" + '\033[0m'

This is a purple output

或者,您可以使用colorama包 : >>> from colorama import Fore

>>> print Fore.LIGHTMAGENTA_EX + "This is a purple output" + Fore.RESET

This is a purple o

...

应该避免在代码中明确地使用硬编码的数字,这称为魔术数字 ,而您可以很少标记该数字的使用。 常量 public const int Megabyte = 1024 * 1024;

public const long Billion = 1000000000; // Or: (long)1E+9;

枚举 public enum MagicNumbers : long

{

Billion = 1000000000

}

下划线 (C#7),@ Lasse V. Karlsen在评论中提及。 l

...

您应该干净地将输入/输出的逻辑与验证逻辑分开(这基本上适用于任何代码)。 这还包括您只将定义放在模块的顶层。 实际程序通常会进入条件,如下面的代码所示,只有在直接从命令行调用文件时才会执行(而不是仅由另一个文件import )。 网格的尺寸可以从输入中导出。 这里不需要单独的参数。 您应该使用整数而不是字符串(这是可选的,但更干净,IMO) 我的尝试(从STDIN获取文件,可以像python script.py < 17.txt一样python script.py < 17.txt ): impo

...

%w分割空白,所以你可以随时插入换行符并得到相同的结果: dependencies = %w(survey survey_section question_group question

dependency dependency_condition answer answer_type answer_validity

validation validation_condition validation_prefix

validation_precludes error error

...

我只是一个开始Haskell程序员(和我学习5年前的小Haskell),但是一开始,我会写出函数的自然翻译,累加器(“当前段落”)被传递(为了清晰起见,我添加了类型): type Line = String

type Para = [Line]

-- Takes a list of lines, and returns a list of paragraphs

paragraphs :: [Line] -> [Para]

paragraphs ls = paragraphs2 ls []

--

...

所以你希望它在你的实际代码中可读吗? Python将隐式地连接代码中的相邻字符串。 要使它跨行工作,您可以使用行继续符\ ,或者,最好将其包装在括号中。 a = ("This is a really long\n"

"line of text that I\n"

"need to show to the user.")

print(a)

So you want it readable in your actual code? Python will implicitly c

...

通过简单地调用list来替换列表理解,可以使代码更加pythonic: print(list(hist_full_g(img.scale(12,44))))

从来没有好的理由要做: [x for x in iterable]

因为list(iterable)产生相同的结果。 您可能还想使用两行来分解代码: hist = hist_full_g(img.scale(12, 44))

print(list(hist))

# or

hist = list(hist_full_g(img.scale(

...

所以我设法解决了这个问题,对于任何在未来偶然发现这一点的人来说。 您可以像这样定义段落。 虽然它肯定不理想,但并不完全符合我所描述的语法。 相关代码是: line = OneOrMore(CharsNotIn('\n')) + Suppress(lineEnd)

emptyline = ~line

paragraph = OneOrMore(line) + emptyline

paragraph.setParseAction(join_lines)

其中join_lines定义为: def jo

...

如果你的字符串只包含段落,你可以用一个精心制作的regex和re.split() 。 但是,如果您的字符串是更复杂的HTML,或者并不总是有效的HTML,您可能需要查看BeautifulSoup包。 用法如下所示: from BeautifulSoup import BeautifulSoup

soup = BeautifulSoup(some_html)

paragraphs = list(unicode(x) for x in soup.findAll('p'))

If your st

...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值