我是 Python 新手,我正在使用我在此处找到的示例之一从文件中读取行并打印它们。我不明白为什么解释器会忽略\n
转义序列:
文本文件:
以下哪些是您可能会在 PC 中找到的组件?(选择所有正确答案。)
A、中央处理器
B、主板
C. 键盘
答案:A、B 和 E。\n PC 内的常见组件包括\n CPU、主板和\n RAM
Python代码:
questions_fname="Test.txt"
with open(questions_fname, 'r') as f:
questions = [line.strip() for line in f]
for line in questions:
print (line)
f.close()
我得到的结果是这样的字符串:
Answers: A, B, and E. \nCommon components inside a PC include \nthe CPU,motherboard, and \nRAM
我只是在寻找一种简单的方法来格式化长线以适应屏幕。