简体   繁体   English

Python 计算字符串的出现次数并打印包含它们的行,以及打印字符串出现的次数,带有多个子句

[英]Python count occurences of a string and print the lines that contain them, as well as print number of occurences of string, with multiple clauses

I've been trying to create a python script that takes in two inputs, a file name and a string.我一直在尝试创建一个 python 脚本,它接受两个输入,一个文件名和一个字符串。 Once it takes those in, it is supposed to print out the number of occurrences of the input string, as well as each line that contains the input string.一旦将这些输入,它应该打印出输入字符串的出现次数,以及包含输入字符串的每一行。

I am also required to not use lists, the split method, python string methods, the keyword "in", and i may only use indexing to access the first character of a string, and slicing to get the tail of the string.我还被要求不使用列表、split 方法、python 字符串方法、关键字“in”,并且我可能只使用索引来访问字符串的第一个字符,并使用切片来获取字符串的尾部。

What I've done so far:到目前为止我所做的:

def main():
  search_for = raw_input("What term would you like to search for?")
  text_file_name = raw_input("Which file would you like to search in?")
  count_text_file(search_for, text_file_name)




def count_text_file(search_for, text_file_name):
   usersFile = open(text_file_name, 'r')
   usersTermLength = len(search_for)
   usersFileLength = len(text_file_name)

   occurenceOfString = 0

    while i<usersFileLength:
        firstChar = usersFile[i]
        if firstChar==searchFor[0]:
            indexUsersTermLength = usersTermLength + i #end slice
            possibleMatch = usersFile[i:indexUsersTermLength]
            if possibleMatch == searchFor:
                print #the entire line
                occurenceOfString+=1
                i+=1
            else: 
                i+=1
        else:
            i+=1

A few issues in your code.您的代码中的一些问题。

usersFileLength = len(text_file_name)

That's just the length of the name of the file.这只是文件名的长度。 Not the size of the file's contents.不是文件内容的大小。

firstChar = usersFile[i]

This is not how you read from a file.这不是您从文件中读取的方式。 You need to use a function likeread() .您需要使用read()类的函数。

Also, you break a couple of the (silly) constraints.此外,您打破了一些(愚蠢的)约束。 Here's my solution.这是我的解决方案。 It reads the entire file then goes through it char-by char.它读取整个文件,然后逐个字符地遍历它。 It builds the current word and when it reaches a non-letter it compares.它构建当前单词,当它到达一个非字母时进行比较。

def count_text_file(search_for, text_file_name):
    with open(text_file_name, 'r') as users_file:
        # Read entire file
        content = users_file.read()
        line_number = 1
        # Build the words of the file char-by-char
        current_word = ""
        while len(content) > 0:
            # "only use indexing to access the first character of a string"
            c = content[0]
            # If it's a letter add to string
            # Can't use c.isalpha() as it is a "python string method"
            if (c >= 'A' and c <= 'Z') or (c >= 'a' and c <= 'z'):
                current_word += c
            # Else (not a letter), check the word
            else:
                if current_word == search_for:
                    print(f"found at line {line_number}")
                if c == '\n':
                    line_number += 1
                # Reset for next word
                current_word = ""
            # "only use ... slicing to get the tail of the string"
            content = content[1:]

There are some improvements you can do.您可以进行一些改进。 For example, words with punctuation in them won't be found (ex: "can't" or "non-existent").例如,找不到标点符号的单词(例如:“不能”或“不存在”)。 Also, it only considers a "letter" to be "[A-Za-z]".此外,它只将“字母”视为“[A-Za-z]”。 Unicode characters won't be recognized. Unicode 字符将无法识别。 And it is case sensitive.并且区分大小写。 But since this is an assignment, who knows if your teacher cares about any of that.但既然这是一项作业,谁知道你的老师是否关心这些。

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

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