在 Python PEP 8 的字符串中使用反斜杠时使日志输出漂亮?

Make logging output pretty when using backslash in string for Python PEP 8?

我有一个 CLI 脚本,它使用日志记录模块打印到屏幕和文件。使用 \ 打破 PEP8 的长行时,是否可以使输出 看起来更好

logger.warning("SKIPPED File: '%s'; \
    MyFunc() returned no results."
    % (dir_file, ))

                               # You have to scroll to see the result --> 
WARNING----SKIPPED File: 'test-filetypes/client-somefile.txt';                            MyFunc() returned no results.

每个字符串旁边的字符串会自动附加,因此

logger.warning("SKIPPED File: '%s'; " \
   "MyFunc() returned no results."
    % (dir_file, ))

应该能满足您的需求。

是,使用隐式字符串连接:

logger.warning(
    "SKIPPED File: '%s'; "
    "MyFunc() returned no results.",
    dir_file,
)

注意:我在这里修复了另一个细节 - 你不应该急于格式化你的日志字符串。只需将模板变量作为日志调用中的参数传递即可。