编写程序,读取" pm . txt "的内容,去除空行和注释行后,以行为单位进行升序排列,并将结果输出 到屏幕上,输出结果为: A Grain of Sand And a heaven in a wild flower , And eternity in an hour . Hold infinity in the palm of your hand , To see a world in a grain of sand ,
时间: 2023-12-14 15:36:31 浏览: 128
程序的出出结果
以下是 Python 代码实现:
```python
# 读取文件内容
with open('pm.txt', 'r') as f:
content = f.readlines()
# 去除空行和注释行
lines = []
for line in content:
# 去除空白字符,判断是否为空行或注释行
stripped_line = line.strip()
if stripped_line and not stripped_line.startswith('#'):
lines.append(stripped_line)
# 升序排列
lines.sort()
# 输出结果
print('\n'.join(lines))
```
运行结果:
```
A Grain of Sand
And a heaven in a wild flower,
And eternity in an hour.
Hold infinity in the palm of your hand,
To see a world in a grain of sand,
```
阅读全文