例子:
def print_text():
print("Here's some text")
print("Here's some more text")
print("Here's the rest of the text")
print_text()
# Hypothetical command that indents all of the following text
start_indenting()
print_text()
# Hypothetical command that stops indenting.
stop_indenting()
print_text()
期望輸出:
Here's some text
Here's some more text
Here's the rest of the text
Here's some text
Here's some more text
Here's the rest of the text
Here's some text
Here's some more text
Here's the rest of the text
我正在尋找可以在不實際更改文本或命令的情況下縮進所有文本的內容。我不知道我將如何實作這一目標。考慮到我在程式中使用的方法有大量的列印陳述句,在給定的方法 (print_text) 中編輯每個列印陳述句將是最后的手段。我看過 textwrap 但它不能做我需要的。
uj5u.com熱心網友回復:
我已經將背景關系管理器用于類似的問題陳述:
class Indenter:
def __init__(self):
self.level = 0
def __enter__(self):
self.level = 1
return self
def __exit__(self, exc_type, exc_val, exc_tb):
self.level -= 1
def print(self, text):
print('\t'*self.level text)
with Indenter() as indent:
indent.print('hi!')
with indent:
indent.print('hello')
with indent:
indent.print('bonjour')
indent.print('hey')
類Indenter可以定制。
uj5u.com熱心網友回復:
如果您只在本地(在單個檔案中)需要它,您可以覆寫列印功能:
from builtins import print as default_print
print = default_print
def indented_print(*args, prefix=' ', **kwargs):
default_print(prefix, *args, **kwargs)
def start_indenting():
global print
print = indented_print
def stop_indenting():
global print
print = default_print
def print_text():
print("Here's some text")
print("Here's some more text")
print("Here's the rest of the text")
print_text()
# Hypothetical command that indents all of the following text
start_indenting()
print_text()
# Hypothetical command that stops indenting.
stop_indenting()
print_text()
您還可以影響所有列印(不僅在此模塊中),
def start_indenting():
builtins.print = indented_print
但是您的模塊的用戶可能不會期望它,并且在出現問題時可能很難追溯。
uj5u.com熱心網友回復:
沒有匯入就沒有這樣的命令。你可以自己編碼:
def my_print(text, indent=0, space=4):
if indent:
print(indent * space * ' ', end='', sep='')
print(text)
def print_text(indent = 0):
my_print("Here's some text", indent)
my_print("Here's some more text", indent)
my_print("Here's the rest of the text", indent)
print_text()
print_text(1)
print_text(2)
print_text(3)
print_text(1)
print_text()
輸出:
Here's some text
Here's some more text
Here's the rest of the text
Here's some text
Here's some more text
Here's the rest of the text
Here's some text
Here's some more text
Here's the rest of the text
Here's some text
Here's some more text
Here's the rest of the text
Here's some text
Here's some more text
Here's the rest of the text
Here's some text
Here's some more text
Here's the rest of the text
顯然text,如果您的用例需要這些,您需要為多行添加處理(可能將其拆分并將其應用于所有行)以及格式化。
對于匯入,您可以使用textwrap.indent。
uj5u.com熱心網友回復:
有很多方法可以實作縮進。一個簡單的示例是創建您自己的列印函式,該函式接受縮進作為引數。
def print_line(text, indent=0):
line = " " * indent text
print(line)
def print_text():
print_line("Here's some text")
print_line("Here's some more text")
print_line("Here's the rest of the text")
print_line("Here's some text", indent=4)
print_line("Here's some more text", indent=4)
print_line("Here's the rest of the text", indent=4)
print_line("Here's some text")
print_line("Here's some more text")
print_line("Here's the rest of the text")
還有像str.ljust和str.rjust這樣的字串方法可以用任何字符填充文本,包括空格和制表符。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/403631.html
標籤:
