Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

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 ?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

This has nothing to do with python3 -c or semicolons or backslashes, actually. Your problem is that you’re printing a list of strings, i.e. the for-loop is inside the list comprehension. String elements inside of a list will always be represented using their repr, which means newline characters will be escaped.

Instead, just use a loop. Literal newlines are fine for python3 -c. Let the print function append the trailing new line. 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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading