请告诉我sting-value formating的最佳实践

时间:2016-02-04 11:08:23

标签: python django pep

我有一个像下面这样的长字符串,我试图格式化它与PEP标准对齐

$ rails s
=> Booting Puma
=> Rails 5.0.0.beta2 application starting in development on http://localhost:3000
=> Run rails server -h for more startup options
=> Ctrl-C to shutdown server
Puma 2.16.0 starting...

    Min threads: 0, max threads: 16
    Environment: development
    Listening on tcp://localhost:3000 Started POST "/teams" for 127.0.0.1 at 2016-02-04 16:25:06 +0530 ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations".* FROM "schema_migrations"

ActionController::RoutingError (uninitialized constant TeamsController):
activesupport (5.0.0.beta2) lib/active_support/inflector/methods.rb:259:in const_get' activesupport (5.0.0.beta2) lib/active_support/inflector/methods.rb:259:inblock in constantize'
activesupport (5.0.0.beta2) lib/active_support/inflector/methods.rb:257:in each' activesupport (5.0.0.beta2) lib/active_support/inflector/methods.rb:257:ininject'
activesupport (5.0.0.beta2) lib/active_support/inflector/methods.rb:257:in constantize' actionpack (5.0.0.beta2) lib/action_dispatch/http/request.rb:93:incontroller_class'
actionpack (5.0.0.beta2) lib/action_dispatch/routing/route_set.rb:44:in controller' actionpack (5.0.0.beta2) lib/action_dispatch/routing

但是输入就像这样输出

'http://abc/api/run=1&'+ \
            'actionData=[{"query":"air-{0}-{1}-{2}--1-0-0-E-111--"},{"query":"hotels-{1}-{3}-{4}-1-1_0-"},{"query":"air-{1}-{0}-{5}--1-0-0-E-111--"}]'+ \
            '&tripOrigin={0}&tripDestination={1}'.format(origin.get('vcid'), destination.get('vcid'), onward_f_date, check_in_date, check_out_date, return_f_date)

期望格式化完成所有{0},{1},{2},{3},{4}和{5}

4 个答案:

答案 0 :(得分:1)

不要使用连接:使用括号内的隐式连接。

my_string = (
    'http://abc/api/run=1&'
    'actionData=[{"query":"air-{0}-{1}-{2}--1-0-0-E-111--"},{"query":"hotels-{1}-{3}-{4}-1-1_0-"},{"query":"air-{1}-{0}-{5}--1-0-0-E-111--"}]'
    '&tripOrigin={0}&tripDestination={1}'.format(origin.get('vcid'), destination.get('vcid'), onward_f_date, check_in_date, check_out_date, return_f_date)
)

答案 1 :(得分:1)

用括号括起你的字符串,你也不再需要+和\:

('http://abc/api/run=1&'
 'actionData=[{"query":"air-{0}-{1}-{2}--1-0-0-E-111--"},{"query":"hotels-{1}-{3}-{4}-1-1_0-"},{"query":"air-{1}-{0}-{5}--1-0-0-E-111--"}]'
 '&tripOrigin={0}&tripDestination={1}').format(origin.get('vcid'), destination.get('vcid'), onward_f_date, check_in_date, check_out_date, return_f_date)

答案 2 :(得分:1)

你需要逃避与格式化占位符不对应的花括号。

例如: 此

'actionData=[{"query":"air-{0}-{1}-{2}--1-0-0-E-111--"}

应该是这样的:

'actionData=[{{"query":"air-{0}-{1}-{2}--1-0-0-E-111--"}}

所以:

>>> print 'http://abc/api/run=1&actionData=[{{"query":"air-{0}-{1}-{2}--1-0-0-E-111--"}},{{"query":"hotels-{1}-{3}-{4}-1-1_0-"}},{{"query":"air-{1}-{0}-{5}--1-0-0-E-111--"}}]&
tripOrigin={0}&tripDestination={1}'.format(999,999,999,999,999,999)

http://abc/api/run=1&actionData=[{"query":"air-999-999-999--1-0-0-E-111--"},{"query":"hotels-999-999-999-1-1_0-"},{"query":"air-999-999-999--1-0-0-E-111--"}]&tripOrigin=999&tripDestination=999

答案 3 :(得分:1)

my_string = (
    'http://abc/api/run=1&'
    'actionData=[{"query":"air-{0}-{1}-{2}--1-0-0-E-111--"},'
    '{"query":"hotels-{1}-{3}-{4}-1-1_0-"},'
    '{"query":"air-{1}-{0}-{5}--1-0-0-E-111--"}]'
    '&tripOrigin={0}&tripDestination={1}'
).format(origin.get('vcid'), destination.get('vcid'),
         onward_f_date, check_in_date, check_out_date, return_f_date)