【问题标题】:How to avoid additional whitespaces in loggerfile, caused by indentions如何避免由缩进引起的记录器文件中的额外空格
【发布时间】:2015-06-25 12:14:08
【问题描述】:

今天我在我的项目中应用了PEP 8 编码约定。因此我拆分了我的logger.debug 以避免E501 line to long。这就是我现在拥有的:

def find_window():
    ...
    logger.debug("Returning a List of Window handles\
                with specified Windowtitle: {}".format(title))

到目前为止,一切都很好,但坏消息是,这是我在 loggerfile 中得到的:

06/25/2015 02:07:20 PM - DEBUG - libs.Window on 104 : Returning a List of Window handles with specified Windowtitle: desktop: notepad

单词句柄后还有额外的空格。我知道我是否会这样做:

def find_window():
    ...
    logger.debug("Returning a List of Window handles \
with specified Windowtitle: {}".format(title))

这会起作用,但如果你有更多的缩进,它看起来很愚蠢甚至更多。 如何避免在我的 loggerfile 中出现这些额外的空格?

【问题讨论】:

    标签: python logging coding-style pep8


    【解决方案1】:
    logger.debug((
                  "Returning a list of Window handles"
                  "with specified Windowtitle: {}"
                 ).format(title))
    

    【讨论】:

      【解决方案2】:

      这可以是另一种方式

      import re
      logger.debug(re.sub('\s+', ' ', "Returning a List of Window handles\
                                       with specified Windowtitle: {}".format(title)))
      

      【讨论】:

        猜你喜欢
        • 2012-01-26
        • 2011-11-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-01-27
        • 1970-01-01
        • 2016-07-19
        • 2021-04-09
        相关资源
        最近更新 更多