在保持良好可读性和空格的同时格式化大量字符串的更pythonic方法是什么?



我正在从事一个疯狂的lib项目。我有一个我想打印的故事,但是每当我使用三引号来定义我的字符串时,我都会得到不必要的缩进。当我尝试通过删除用来保持整洁且可读的标签和空间来克服此问题时,它看起来只是不愉快。我最终使用了下面的代码,但我认为也许有更好的方法可以做到这一点。会有更多的Pythonic进行格式化?

我最终得到的代码:

name, place1, place2, adj1, adj2, adj3, pNoun1, pNoun2, pNoun3, pNoun4,
aVerb1, aVerb2, aVerb3, noun = None
print ('Last summer, my mom and dad took me and %s on a trip to %s. ', % name, place1,
       'The weather there is very %s! Northern %s has many %s, and ', % adj1, place1, pNoun1
       'they make %s %s there. Many people also go to %s to %s or see ', % adj2, pNoun2, place2, aVerb1
       'the %s. The people that live there love to eat %s and are very ', % pNoun3, pNoun4
       'proud of their big %s. They also liketo %s in the sun and in the ', % noun, aVerb2
       '%s! It was a really %s trip!' % aVerb3, adj3)

起初我这样做了

print('''Last summer, my mom and dad took me and %s on a trip to %s.
     The weather there is very %s! Northern %s has many %s, and they
     make %s %s there. Many people also go to %s to %s or see the %s.
     The people that live there love to eat %s and are very proud of
     their big %s. They also liketo %s in the sun and in the %s! It 
     was a really %s trip!''' %  (name, place1, adj1, place1, 
     pNoun1,adj2, pNoun2, place2, aVerb1, pNoun3, pNoun4, noun, aVerb2,
     aVerb3, adj3)) 

您可以将formatAccessing arguments by name一起使用,请参阅文档。

尝试:

infos = {
    'name': 'name',
    'noun': 'noun',
    'adj1': 'adj1',
    'adj2': 'adj2',
    'adj3': 'adj3',
    'aVerb1': 'aVerb1',
    'aVerb2': 'aVerb2',
    'aVerb3': 'aVerb3',
    'place1': 'place1',
    'place2': 'place2',
    'pNoun1': 'pNoun1',
    'pNoun2': 'pNoun2',
    'pNoun3': 'pNoun3',
    'pNoun4': 'pNoun4',
}
print('''Last summer, my mom and dad took me and {name} on a trip to %s.
     The weather there is very {adj1}! Northern {place1} has many {pNoun1}, and they
     make {adj2} {pNoun2} there. Many people also go to {place2} to {aVerb1} or see the {pNoun3}.
     The people that live there love to eat {pNoun4} and are very proud of
     their big {noun}. They also liketo {aVerb2} in the sun and in the {aVerb3}! It 
     was a really {adj3} trip!'''.format(**infos))

您可以以格式重复使用名称参数更灵活:

print('{pNoun1} {aVerb1} {pNoun1}'.format(**infos))
=> pNoun1 aVerb1 pNoun1

尝试使用格式()方法格式化的新样式。这样的工作:

print ("Last summer, my mom and dad took me and {} on a trip to 
{}.".format(name, place1))

@cthesky的答案还可以,但是我更喜欢使用字符串中的模板,它具有一个方法 safe_substitute ,如果缺少某些键,可以处理该情况。

不例外。
infos = {
    'name': 'XXX',
    'noun': 'noun',
    'adj1': 'adj1',
    'adj2': 'adj2',
    'adj3': 'adj3',
    'aVerb1': 'aVerb1',
    'aVerb2': 'aVerb2',
    'aVerb3': 'aVerb3',
    'place1': 'place1',
    'place2': 'place2',
    'pNoun1': 'pNoun1',
    'pNoun2': 'pNoun2',
    'pNoun3': 'pNoun3',
    'pNoun4': 'pNoun4',
}
from string import Template
tpl = Template('''Last summer, my mom and dad took me and $name on a trip to %s.
     The weather there is very $adj1! Northern $place1 has many $pNoun1, and they
     make $adj2 $pNoun2 there. Many people also go to $place2 to $aVerb1 or see the $pNoun3.
     The people that live there love to eat $pNoun4 and are very proud of
     their big $noun. They also like to $aVerb2 in the sun and in the $aVerb3! It 
     was a really $adj3 trip!''')
print(tpl.safe_substitute(**infos))

至少有两种通用方法避免在三重引用文本中额外缩进。

从第0列开始写文本:

def func():
    txt = """
line1
line2
line3
"""
    pass

可以说是丑陋的。

或使用destent。链接中包含一个示例。

相关内容

最新更新