简体   繁体   English

在文件中查找单词

[英]Finding a Word in a File

I'm trying to read from a file 'input.txt', one line at a time, numbering each line. 我正在尝试从文件“ input.txt”中读取,一次一行,每行编号。 I then need to check each line to see whether the letters in the line can be used to make the word 'aardvark'. 然后,我需要检查每一行,以查看该行中的字母是否可用于使单词“ aardvark”。 Uppercase letters can be used as well and the letters can be spread throughout the line. 也可以使用大写字母,并且这些字母可以分布在整行中。 The text file's contents are: 文本文件的内容为:

No aardv*rks here!

Only armadillos and anteaters.

Animals are run down: very awful road kill.

I prefer a quick guacamole made from avocados.

So line 3 and 4 only should return a positive, such as "Aardvark on line 3". 因此,第3行和第4行仅应返回正数,例如“第3行上的Aardvark”。

I've gotten this far - and I'm stuck! 我已经走了这么远-而且我被卡住了!

file = open('input.txt').read().lower() 
listed = file.split('\n')  
for sentence in listed:
  a = sentence.count('a')
  r = sentence.count('r')
  d = sentence.count('d')
  v = sentence.count('v')
  k = sentence.count('k')
  if a==3 and r==2 and d==1 and v==1 and k==1:
    print('Aardvark on line '+listed[sentence])

Please understand that I'm working through beginner tutorials, so no wild and fancy functions that will blow my tiny, fragile mind, please!! 请理解我正在通过初学者教程学习,所以请不要使用任何狂野和幻想的功能,这些东西会打击我的微小,脆弱的头脑!

Many thanks in anticipation. 非常感谢您的期待。

EDIT 编辑

This is the solution that finally worked. 这是最终有效的解决方案。 Many thanks for the contributions once again! 再次感谢您的贡献!

file = open('input.txt').read().lower() 
listed = file.split('\n')  
for index, sentence in enumerate(listed):
  a = sentence.count('a')
  r = sentence.count('r')
  d = sentence.count('d')
  v = sentence.count('v')
  k = sentence.count('k')
  if a>=3 and r>=2 and d>=1 and v>=1 and k>=1:
    print('Aardvark on line '+str(index+1))

open('input.txt').read() reads the entire content of the file into memory. open('input.txt').read()将文件的全部内容读入内存。 Thus, if input.txt is a long enough file the computer will exhaust its available memory. 因此,如果input.txt是足够长的文件,则计算机将耗尽其可用内存。 Instead, try reading the lines one at a time using the file iterator, eg 相反,尝试使用文件迭代器一次读取一行。

for i,listed in enumerate(open('input.txt')):
    sentence = listed.lower()
    a = sentence.count('a')
    # ... etc.

See also In Python, why do we need readlines() when we can iterate over the file handle itself? 另请参见在Python中,为什么我们可以在文件句柄本身上进行迭代时需要readlines()? .

for n, line in enumerate(open('input.txt')):
  letters = list('aardvark')
  for c in line.lower():
    if c in letters:
      letters.remove(c)
  if not letters:
    print('Aardvark on line', n + 1)

I know this is an old thread, but for those undertaking this task through Grok the following code uses only what has been taught so far in the course. 我知道这是一个旧线程,但是对于那些通过Grok执行此任务的人,以下代码仅使用本课程到目前为止所学的内容。 Hope you find it useful! 希望你觉得它有用!

count = 0
with open('input.txt') as f:
  for line in f:
    count += 1
    l = line.lower()
    a = l.count('a')
    d = l.count('d')
    k = l.count('k')
    r = l.count('r')
    v = l.count('v')
    if a >= 3 and d >= 1 and k >= 1 and r >=2 and v >=1:
      print('Aardvark on line', count)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM