Python 用换行符漂亮地打印元组
如何使其打印:Python 用换行符漂亮地打印元组,python,Python,如何使其打印: >>> print(("hello\nworld", "hello2")) ('hello\nworld', 'hello2') 我的意思是,它不能将\n打印为符号,而是实现此符号并生成新行 Python版本是3.4 我尝试使用pprint,但效果相同: ('hello world', 'hello2') 写作: >>> import pprint >>> pp = pprint.PrettyPrinter(indent=
>>> print(("hello\nworld", "hello2"))
('hello\nworld', 'hello2')
我的意思是,它不能将\n
打印为符号,而是实现此符号并生成新行
Python版本是3.4
我尝试使用pprint
,但效果相同:
('hello
world', 'hello2')
写作:
>>> import pprint
>>> pp = pprint.PrettyPrinter(indent=4)
>>> pp.pprint(("hello\nworld"))
'hello\nworld'
将逐字打印:
print("('Hello\nworld', 'hello2')")
如果您只是想在字符串中插入新行,请使用以下命令:
('hello
world', 'hello2')
\n是新行的转义序列,终止当前行并发出下一行开始的信号
要将其与现有代码进行比较,应注意“符号”的位置,表示字符串的开头和结尾。如果不需要括号和逗号,只需使用
*
运算符即可:
print("Line1\nLine2")
如果您想让它打印括号和逗号,同时将'\n'
转换为换行符,您必须对该行为进行编码,如@Peter所说
>>> t = ("hello\nworld", "hello2")
>>> print(*t)
hello
world hello2
没有任何东西可以为您自动执行这种打印。Python容器默认使用
repr
将其内容转换为字符串(即使您在容器上调用str
,而不是repr
)。这是为了避免像[“foo,bar”,“baz”]
(如果没有包括引号,则无法判断列表中是否有两个或三个项目)
但是,您可以对元组进行自己的格式化,并获得所需的输出:
>>> print('(' + ', '.join(t) + ')')
(hello
world, hello2)
如果字符串包含“
标记,则这将不正确
请注意,Python的格式在处理包含引号的字符串时做了一些巧妙的事情。下面是一个更复杂的示例,它来自一些让我恼火的Ansible输出:
>>> t = ("hello\nworld", "hello2")
>>> print '({})'.format(', '.join("'{}'".format(value) for value in t))
('hello
world', 'hello2')
问题是
pprint
会转义换行符,所以我只需取消换行即可。您可以执行以下操作:
import pprint
f={
"failed": True,
"msg": "the field 'args' has an invalid value, which appears to include a variable that is undefined. The error was: 'dict object' has no attribute 'uid'\n\nThe error appears to have been in '/usr/local/etc/ansible/roles/singleplatform-eng.users/tasks/main.yml': line 7, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: Per-user group creation\n ^ here\n"
}
def ppdump(data):
print pprint.pformat(data, indent=4, width=-1).replace('\\n', '\n')
ppdump(f)
{ 'failed': True,
'msg': "the field 'args' has an invalid value, which appears to include a variable that is undefined. The error was: 'dict object'
has no attribute 'uid'
The error appears to have been in '/usr/local/etc/ansible/roles/singleplatform-eng.users/tasks/main.yml': line 7, column 3, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
- name: Per-user group creation
^ here
"}
参考此我认为您不能同时打印数据结构和计算数据。您必须自己实现它。没有注意到您在每个字符串周围包含了
”
。如果您需要,请使用@Jeremie的答案。您必须确保按照您希望的方式格式化元组-“('1','2'))“
将不同于”('1','2')”
。您可以通过执行打印(*t,sep=','))来获取逗号。
import pprint
f={
"failed": True,
"msg": "the field 'args' has an invalid value, which appears to include a variable that is undefined. The error was: 'dict object' has no attribute 'uid'\n\nThe error appears to have been in '/usr/local/etc/ansible/roles/singleplatform-eng.users/tasks/main.yml': line 7, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: Per-user group creation\n ^ here\n"
}
def ppdump(data):
print pprint.pformat(data, indent=4, width=-1).replace('\\n', '\n')
ppdump(f)
{ 'failed': True,
'msg': "the field 'args' has an invalid value, which appears to include a variable that is undefined. The error was: 'dict object'
has no attribute 'uid'
The error appears to have been in '/usr/local/etc/ansible/roles/singleplatform-eng.users/tasks/main.yml': line 7, column 3, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
- name: Per-user group creation
^ here
"}
v = [(1,2),(2,3),(4,5)]
for item in v:
n1,n2 = item
print(n1,n2)