gpt4 book ai didi

python - 打印出文本的行号?

转载 作者:行者123 更新时间:2023-12-04 01:14:24 25 4
gpt4 key购买 nike

有没有办法在输出中打印出文本行号?

我有一个示例文本(前 3 个句子):

There was no possibility of taking a walk that day

We had been wandering indeed in the leafless shrubbery an hour in the morning but since dinner (Mrs
Reed when there was no company dined early) the cold winter wind had brought with it clouds so sombre
and a rain so penetrating that further out door exercise was now out of the question

I was glad of it I never liked long walks especially on chilly afternoons dreadful to me was the
coming home in the raw twilight with nipped fingers and toes and a heart saddened by the chidings of
Bessie the nurse and humbled by the consciousness of my physical inferiority to Eliza John and
Georgiana Reed

我目前得到这个输出:

9 out of 10 words contain no 'e'.
30 out of 53 words contain no 'e'.
31 out of 56 words contain no 'e'.

但我找不到在开头也包含行号的方法,例如:

0: 9 out of 10 words contain no 'e'.
1: 30 out of 53 words contain no 'e'.
2: 31 out of 56 words contain no 'e'.

代码(尝试使用 enumerate(line) 但不起作用):

with open("jane_eyre_sentences.txt") as line:
low = line.lower()
words = low.split()
app = []
for word in words:
if "e" not in word:
app.append(word)
print("%d:%s"%(i, wordy) + "" + str(len(app)) + " out of " + str(len(words)) + " words contain no 'e'." )

感谢阅读!

最佳答案

你所说的 line 实际上是文件 - 而不是文本行。

对它执行 low = line.lower() 甚至都不起作用:

Exception has occurred: AttributeError
'_io.TextIOWrapper' object has no attribute 'lower'

尝试

with open("jane_eyre_sentences.txt") as file:
for nr, line in enumerate(file):
words = line.lower().split()
words_without_e = [ w for w in words if 'e' not in w]

print(f"{nr}: {len(words_without_e)} out of {len(words)} words contain no 'e'.")

输出:

0: 9 out of 10 words contain no 'e'.
1: 0 out of 0 words contain no 'e'.
2: 8 out of 18 words contain no 'e'.
3: 11 out of 19 words contain no 'e'.
4: 11 out of 16 words contain no 'e'.
5: 0 out of 0 words contain no 'e'.
6: 12 out of 19 words contain no 'e'.
7: 11 out of 19 words contain no 'e'.
8: 8 out of 16 words contain no 'e'.
9: 0 out of 2 words contain no 'e'.

我用过

with open("jane_eyre_sentences.txt","w") as file:
file.write("""There was no possibility of taking a walk that day

We had been wandering indeed in the leafless shrubbery an hour in the morning but since dinner (Mrs
Reed when there was no company dined early) the cold winter wind had brought with it clouds so sombre
and a rain so penetrating that further out door exercise was now out of the question

I was glad of it I never liked long walks especially on chilly afternoons dreadful to me was the
coming home in the raw twilight with nipped fingers and toes and a heart saddened by the chidings of
Bessie the nurse and humbled by the consciousness of my physical inferiority to Eliza John and
Georgiana Reed""")

作为输入文件。

关于python - 打印出文本的行号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63805167/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com