python-3.x - 如何编写一个函数,反向打印 data.txt 中的行
问题描述
我花了几周的时间来正确学习 python,但不知道如何拆分lines[::-1]
以获得所需的结果:
Order will return.
Reflect, repent, and reboot
Chaos reigns within.
从
Chaos reigns within.
Reflect, repent, and reboot
Order will return.
到目前为止,我所做的是以正确的顺序打印出 3 个句子,但其他一切都是错误的。
def print_reversed(filename):
"""prints out the lines from the file in reversed order"""
filename = open(filename)
contents = filename.read()
lines = contents.splitlines()
for line in lines:
print(lines[::-1])
print_reversed('data.txt')
到目前为止我所做的打印出来:
['Order will return.', 'Reflect, repent, and reboot ', 'Chaos reigns within. ']
['Order will return.', 'Reflect, repent, and reboot ', 'Chaos reigns within. ']
['Order will return.', 'Reflect, repent, and reboot ', 'Chaos reigns within. ']
帮助将不胜感激!
解决方案
推荐阅读
- java - 创建rest url以在Java和SpringBoot中调用外部api的最佳方法
- php - WordPress 未从 http 重定向到 https SSL
- javascript - 康威生命游戏 p5js
- java - @VisibleForTesting() 函数在使用时不会触发生产代码的编译错误
- java - 我可以控制 JVM 选项中的异常吗?
- symfony - symfony 学说 2 OneToMany - 与反边场错误的关联
- gulp - 为什么 Gulp 4 基本选项在我当前的文件夹中生成文件
- javascript - 这个动作创建者在做什么?
- regex - 在一行中的两个单词之间替换正则表达式模式
- android - 如何更改自定义列表适配器中的 ListView 数据?