[英]How to print a newline when running 'python -c'
The following prints the 2-characters \
and n
between each item in the list instead of a newline.以下打印列表中每个项目之间的 2 个字符
\
和n
而不是换行符。
python3 -c 'import airflow.operators as air_ops; \
print([f"""{k}: {v}\n""" for k,v in air_ops.__dict__.items()])'
How can we get the desired escape character/newline when running python -c
?运行
python -c
时,我们如何获得所需的转义字符/换行符?
This has nothing to do with python3 -c
or semicolons or backslashes, actually.实际上,这与
python3 -c
或分号或反斜杠无关。 Your problem is that you're printing a list of strings, ie the for-loop is inside the list comprehension.你的问题是你正在打印一个字符串列表,即 for 循环在列表推导中。 String elements inside of a list will always be represented using their
repr
, which means newline characters will be escaped.列表中的字符串元素将始终使用其
repr
表示,这意味着换行符将被转义。
Instead, just use a loop.相反,只需使用一个循环。 Literal newlines are fine for
python3 -c
.文字换行符适用于
python3 -c
。 Let the print function append the trailing new line.让打印 function append 尾随新行。 You can leave out all the semicolons and backslashes.
您可以省略所有分号和反斜杠。
$ python3 -c 'd = {"k1": "v1", "k2": "v2"}
> for k, v in d.items():
> print(f"{k}: {v}")
> '
k1: v1
k2: v2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.