【问题标题】:Why are the functions I have set up not working in python?为什么我设置的函数在 python 中不起作用?
【发布时间】:2020-10-07 03:07:50
【问题描述】:

我对 python 还很陌生,我想弄清楚为什么我的函数没有给我我正在寻找的输出。我觉得我已经尝试了一切并尝试研究,但我没有找到任何对我有帮助的东西。

这是我的代码:

from sys import argv                    

def GetCharactersWords(input):
    characters = input.read()
    words = len(characters.split())              
    #word = len(words)
    return words

def GetCharacterNumber(input):
    characters = input.read()
    number = characters.count(".")       
    return number                            

def FirstSentence(input):
    characters = input.read()           
    fields = characters.split(".")        
    field1 = fields[0]                  
    return field1


def LengthFirstSentence(input):
    characters = input.read()
    fields = characters.split(".")
    field1 = fields[0]
    field0 = len(field1)
    return field0

def LastSentence(input):
    characters = input.read()
    fields = characters.split(".")
    field2 = fields[-1]
    return field2                  


def LengthLastSentence(input):
    characters = input.read()
    fields = characters.split(".")
    field3 = fields[-1]                  
    field4 = len(field3)
    return field4


def print_to_file(word_characters, number_characters, first_sentence, length_first,
last_sentence, length_last):
    with open("output.txt", "w") as outfile: 
        outfile.write("Here is the text file: " + '\n') 
        outfile.write(str(input) + '\n')               
        outfile.write("Number of words in the file?" + '\n')  
        outfile.write(str(word_characters) + '\n')
        outfile.write("Number of sentences in the file?" + '\n')
        outfile.write(str(number_characters) + '\n')
        outfile.write("First sentence in the file?" + '\n')
        outfile.write(str(first_sentence) + '\n')
        outfile.write("Last sentence in the file?" + '\n')
        outfile.write(str(last_sentence) + '\n')
        outfile.write("Length of the first sentence" + '\n')
        outfile.write(str(length_first)+ '\n')
        outfile.write("Length of the last sentence" + '\n')
        outfile.write(str(length_last) + '\n')
        outfile.close()                             



def main(argv):                         
    script = argv[0]                    
    filename = argv[1]                  
    input = open(filename, "r")       
    word_characters = GetCharactersWords(input)
    number_characters = GetCharacterNumber(input)
    first_sentence = FirstSentence(input)
    length_first = LengthFirstSentence(input)
    last_sentence = LastSentence(input)
    length_last = LengthLastSentence(input)
    file_print = print_to_file(word_characters, number_characters, first_sentence, length_first,
    last_sentence, length_last)



if __name__ == '__main__':
        main(argv)

这是它给我的输出:

Here is the text file: 
<built-in function input>
Number of words in the file?
2430
Number of sentences in the file?
0
First sentence in the file?

Last sentence in the file?

Length of the first sentence
0
Length of the last sentence
0

我不明白为什么它没有给我不同的输出。

感谢您的帮助!

【问题讨论】:

标签: python function


【解决方案1】:

您的代码存在一些问题。最大的问题是您没有正确读取文件。当你用open()打开一个文件时,你只能read()这个文件一次。调用read() 将读取整个文件并清空流,因此第二次调用read() 只会给您一个空字符串,这就是为什么只有“字数”有非空结果的原因。

相反,您应该调用一次read() 来读取内容,并直接将文件的内容而不是文件句柄传递给您的函数。例如:

# Take characters directly instead of a file
def FirstSentence(characters):
    fields = characters.split(".")        
    field1 = fields[0]                  
    return field1

def main(argv):                         
    script = argv[0]                    
    filename = argv[1]                  
    characters = open(filename, "r").read()
    word_characters = GetCharactersWords(characters) # and so on

另外,“”是为文件内容打印的,因为当你打印它时,input还没有被分配,所以它最终打印了内置函数input。正如其他人指出的那样,input 是一个错误的变量名,因为它覆盖了内置的 input 函数。如果您选择了其他名称,则会生成 NameError

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-06
    • 2017-09-23
    • 2020-12-16
    • 2019-10-06
    • 2019-11-06
    • 2019-03-11
    • 1970-01-01
    相关资源
    最近更新 更多