python日志意外地打印缩进(python log unexpectedly prints the indentation)

编程入门 行业动态 更新时间:2024-10-23 22:37:39
python日志意外地打印缩进(python log unexpectedly prints the indentation)

代码如下:

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.

更多推荐

本文发布于:2023-04-27 14:52:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1327184.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:日志   意外地   python   log   indentation

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!