【发布时间】:2017-08-29 02:07:39
【问题描述】:
我有一个 .txt 文档,其中一些单词位于不同的行
例如:
hello
too
me
我试图弄清楚如何打印每一行,每个单词所在的行号从 1 开始,而不是从 0 开始
想要的输出:
1 = hello
2 = too
3 = me
我已经有了从文本文档中取出线条的解决方案
open_file = open('something.txt', 'r')
lines = open_file.readlines()
for line in lines:
line.strip()
print(line)
open_file.close()
我知道我可以打印出每个单词所在的索引,除非我弄错了,否则行号将从 0 而不是 1 开始。在此致谢
【问题讨论】:
-
你说:我可以打印出每个单词的索引。你应该展示你将如何做到这一点......
-
为什么不能给索引加1?
-
print(index + 1, ' = ', line)有问题吗? -
老实说,我什至没有意识到你可以 + 1 列表索引,因为我对编程很陌生,所以,谢谢