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

时间:2018-10-02 19:37:52

标签: python

我一直在尝试创建一个Python脚本,该脚本接受两个输入,即文件名和字符串。一旦接受了这些内容,就应该打印出输入字符串的出现次数,以及包含输入字符串的每一行。

我还被要求不要使用列表,split方法,python字符串方法,关键字“ in”,并且我只能使用索引来访问字符串的第一个字符,并进行切片以获取字符串的尾部

到目前为止我所做的:

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

1 个答案:

答案 0 :(得分:0)

代码中的一些问题。

usersFileLength = len(text_file_name)

这只是文件名的长度。不是文件内容的大小。

firstChar = usersFile[i]

这不是您从文件中读取的方式。您需要使用类似read()的函数。

此外,您打破了几个(愚蠢的)约束。这是我的解决方案。它读取整个文件,然后按字符逐个字符处理。它会构建当前单词,并在到达非字母时进行比较。

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:]

您可以做一些改进。例如,找不到包含标点符号的单词(例如:“不能”或“不存在”)。同样,它仅将“字母”视为“ [A-Za-z]”。无法识别Unicode字符。而且区分大小写。但是由于这是一项作业,所以谁知道您的老师是否在乎这些。