我如何分解Python中的这一长串? [英] How can I break up this long line in Python?

查看:162
本文介绍了我如何分解Python中的这一长串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你将如何去格式化一个这样的长线?我希望它不超过80个字符宽:

  logger.info(跳过{0},因为它缩略图已经在我们的系统中作为{1}。。格式(line [indexes ['url']],video.title))

这是我的最佳选择吗?

  url =跳过{0},因为它的缩略图是已经在我们的系统中作为{1}。 
logger.info(url.format(line [indexes ['url']],video.title))


解决方案

这是一个开始。在使用它们的代码之外定义更长的字符串并不是一个坏习惯。这是分离数据和行为的一种方法。你的第一个选择是通过使它们彼此相邻来隐式地连接字符串文字:

 (这是第一行我的文本,
将被加入到第二个。)

行结束继续,这是稍微脆弱,因为这个工程:

 这是我的文本的第一行 \ 
将被加入到第二个。

但是这并不是:

 这是我的文本的第一行,\ 
将被加入到第二个。

查看区别?没有?那么你不会在你的代码中。

隐式连接的缺点是它只能用于字符串,而不能用
变量,所以当你重构时,事情会变得更加多毛。另外,也可以使用连接运算符( +

)显式连接,

 (这是我的文本的第一行,+ 

显式比隐式更好,就像python的禅,但是这将创建三个字符串而不是一个,并且使用两倍的内存:有两个你写的,加上一个是两个连在一起,所以你必须知道什么时候忽略禅宗。好处是你可以在每行上分别应用格式化
的任何子字符串,或者从括号外部向整个字段应用格式化。

最后,你可以使用三重引号字符串:

 这是我的文本
的第一行,它将被连接到一秒钟。

这是我最喜欢的,尽管它的行为与换行而后续行中的任何前导空格将显示在最终的字符串中。你可以用一个转义的反斜杠来消除换行符。

 这是我的文本\ 
的第一行,它将被加入到第二行。

这与上面相同的技术有相同的问题,因为正确的代码只是不正确代码由不可见的空白。



哪一个是最好的取决于你的具体情况,但答案不仅仅是审美,而是一个微妙的不同的行为。

How would you go about formatting a long line such as this? I'd like to get it to no more than 80 characters wide:

logger.info("Skipping {0} because its thumbnail was already in our system as {1}.".format(line[indexes['url']], video.title))

Is this my best option?

url = "Skipping {0} because its thumbnail was already in our system as {1}."
logger.info(url.format(line[indexes['url']], video.title))

解决方案

That's a start. It's not a bad practice to define your longer strings outside of the code that uses them. It's a way to separate data and behavior. Your first option is to join string literals together implicitly by making them adjacent to one another:

("This is the first line of my text, "
"which will be joined to a second.")

Or with line ending continuations, which is a little more fragile, as this works:

"This is the first line of my text, " \
"which will be joined to a second."

But this doesn't:

"This is the first line of my text, " \ 
"which will be joined to a second."

See the difference? No? Well you won't when it's your code either.

The downside to implicit joining is that it only works with string literals, not with strings taken from variables, so things can get a little more hairy when you refactor. Also, you can only interpolate formatting on the combined string as a whole.

Alternatively, you can join explicitly using the concatenation operator (+):

("This is the first line of my text, " + 
"which will be joined to a second.")

Explicit is better than implicit, as the zen of python says, but this creates three strings instead of one, and uses twice as much memory: there are the two you have written, plus one which is the two of them joined together, so you have to know when to ignore the zen. The upside is you can apply formatting to any of the substrings separately on each line, or to the whole lot from outside the parentheses.

Finally, you can use triple-quoted strings:

"""This is the first line of my text
which will be joined to a second."""

This is often my favorite, though its behavior is slightly different as the newline and any leading whitespace on subsequent lines will show up in your final string. You can eliminate the newline with an escaping backslash.

"""This is the first line of my text \
which will be joined to a second."""

This has the same problem as the same technique above, in that correct code only differs from incorrect code by invisible whitespace.

Which one is "best" depends on your particular situation, but the answer is not simply aesthetic, but one of subtly different behaviors.

这篇关于我如何分解Python中的这一长串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆