How can I break up this long line in Python?

后端 未结 5 2045
忘了有多久
忘了有多久 2020-11-27 10:03

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 th         


        
相关标签:
5条回答
  • 2020-11-27 10:12

    You can use textwrap module to break it in multiple lines

    import textwrap
    str="ABCDEFGHIJKLIMNO"
    print("\n".join(textwrap.wrap(str,8)))
    

    ABCDEFGH
    IJKLIMNO

    From the documentation:

    textwrap.wrap(text[, width[, ...]])
    Wraps the single paragraph in text (a string) so every line is at most width characters long. Returns a list of output lines, without final newlines.

    Optional keyword arguments correspond to the instance attributes of TextWrapper, documented below. width defaults to 70.

    See the TextWrapper.wrap() method for additional details on how wrap() behaves.

    0 讨论(0)
  • 2020-11-27 10:22

    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.

    0 讨论(0)
  • 2020-11-27 10:22

    Consecutive string literals are joined by the compiler, and parenthesized expressions are considered to be a single line of code:

    logger.info("Skipping {0} because it's thumbnail was "
      "already in our system as {1}.".format(line[indexes['url']],
      video.title))
    
    0 讨论(0)
  • 2020-11-27 10:28

    Personally I dislike hanging open blocks, so I'd format it as:

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

    In general I wouldn't bother struggle too hard to make code fit exactly within a 80-column line. It's worth keeping line length down to reasonable levels, but the hard 80 limit is a thing of the past.

    0 讨论(0)
  • 2020-11-27 10:30

    For anyone who is also trying to call .format() on a long string, and is unable to use some of the most popular string wrapping techniques without breaking the subsequent .format( call, you can do str.format("", 1, 2) instead of "".format(1, 2). This lets you break the string with whatever technique you like. For example:

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

    can be

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

    Otherwise, the only possibility is using line ending continuations, which I personally am not a fan of.

    0 讨论(0)
提交回复
热议问题