代码如下:
self.class_logg('http://example.com/api/?\ option=ajax&nologin=1&a=vmware_migrate_2&\ original_server_ip=%s&target_server_ip=%s&\ vmware_name=%s' % (self.ip, dest_node.ip, machine.name))因为这个字符串太长了,所以我必须把它拆分成几行,但遗憾的是在日志中发现它的打印方式如下:
http://example.com/api/?option=ajax &nologin=1&a=vmware_migrate_2 &original_server_ip=192.168.0.224$显然它会在那里打印缩进空间。 如何解决这个问题? 例如,self.class_logg就在那里,在我的情况下,即使是打印也会出现问题。
The code is as below:
self.class_logg('http://example.com/api/?\ option=ajax&nologin=1&a=vmware_migrate_2&\ original_server_ip=%s&target_server_ip=%s&\ vmware_name=%s' % (self.ip, dest_node.ip, machine.name))Because this string is so long, i have to split it into those several lines, but unfortunately find that in the log it prints like this:
http://example.com/api/?option=ajax &nologin=1&a=vmware_migrate_2 &original_server_ip=192.168.0.224$Apparently it prints the indentation spaces there. How to resolve this problem? the self.class_logg is just there for example, in my case the problem happens even if it is print.
最满意答案
你可以删除\
self.class_logg(('http://example.com/api/?' 'option=ajax&nologin=1&a=vmware_migrate_2&' 'original_server_ip=%s&target_server_ip=%s&' 'vmware_name=%s' % (self.ip, dest_node.ip, machine.name)))在Python中,当字符串在代码中彼此相邻时,它将组合它们。 它被称为“字符串分组”
但是,在我看来,你会用字典做得更好:
fields = {'option': 'ajax', 'nologin': '1', 'a': 'vmware_migrate_2', 'original_server_ip': self.ip, 'target_server_ip': dest_node.ip, 'vmware_name': machine.name} params = '&'.join(['{}={}'.format(k, v) for k, v in fields.items()]) self.class_logg('http://example.com/api/?{}'.format(params))如果订单很重要(不应该这样)您可以使用OrderedDict或元组列表。
You can just remove the \
self.class_logg(('http://example.com/api/?' 'option=ajax&nologin=1&a=vmware_migrate_2&' 'original_server_ip=%s&target_server_ip=%s&' 'vmware_name=%s' % (self.ip, dest_node.ip, machine.name)))In Python, when strings are next to each other in the code, it will combine them. It is called "string grouping"
However, it seems to me that you would do better with a dictionary:
fields = {'option': 'ajax', 'nologin': '1', 'a': 'vmware_migrate_2', 'original_server_ip': self.ip, 'target_server_ip': dest_node.ip, 'vmware_name': machine.name} params = '&'.join(['{}={}'.format(k, v) for k, v in fields.items()]) self.class_logg('http://example.com/api/?{}'.format(params))If the order is important (which it should not be) You can use an OrderedDict, or a list of tuples.
更多推荐
发布评论