Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 编写函数获取文本文件并打印文件中的行数、字数和字符数_Python_File_Io - Fatal编程技术网

Python 编写函数获取文本文件并打印文件中的行数、字数和字符数

Python 编写函数获取文本文件并打印文件中的行数、字数和字符数,python,file,io,Python,File,Io,我目前正在努力解决一个与Python文件I/O相关的问题。我未能完成的问题是: Write a function stats() that takes one input argument: the name of a text file. The function should print, on the screen, the number of lines, words, and characters in the file; your function should open the f

我目前正在努力解决一个与Python文件I/O相关的问题。我未能完成的问题是:

Write a function stats() that takes one input argument: the
name of a text file. The function should print, on the screen, the
number of lines, words, and characters in the file; your function
should open the file only once. 
我还必须消除所有puncuation以正确获取所有单词。函数应按如下方式打印:

>>>stats('example.txt')
line count: 3
word count: 20
character count: 98

您是否对第三方LIB开放

看看:


它具有您想要实现的所有功能。

就您的问题而言,您可以通过以下方式实现:

fname = "inputfile.txt" 
num_lines = 0
num_words = 0
num_chars = 0 
with open(fname, 'r') as f:
    for line in f:
        words = line.split() 
        num_lines += 1
        num_words += len(words)
        num_chars += len(line)

网上有很多免费的python教程。请详细地参考这些。您可以找到参考书

请查看有关I/O、内置函数和常见
string
操作的Python文档

有很多方法可以做到这一点。通过快速尝试,以下内容将根据您的要求完成工作。
split
函数在将每行转换为
列表时将消除空格

def GetFileCounts(in_file):
    char_count = 0
    word_count = 0
    line_count = 0
    with open(in_file) as read_file:
        for line in read_file:
            line_count += 1
            char_count += len(line)
            word_count += len(line.split(' '))
    print "line count: {0}\nword count: {1}\ncharacter count: {2}".format(line_count, word_count, char_count)
您可能希望进一步细化您的需求定义,因为有些微妙的事情可能会改变输出:

  • 你对一个角色的定义是什么?
    • 只有信吗
    • 字母、空格、单词标点符号(如连字符)、行尾等

@nagato,我想你的num\u chars+=len(line)你来自文件、字符和空格,但是谢谢你的代码,我可以自己做

我的


到目前为止你都做了些什么?请在你的问题中加入你的代码,我们不是来帮你完成整个作业/家庭作业的。我得到的最远的结果是用python打开文本文件,我刚刚完成了一个4小时的辅导课程,花费了很多钱,因为我不知道每小时的费率,但辅导老师根本无法帮我开始。我也一直想不出任何方法来解决这个问题。你知道如何从打开的文件对象中删除吗?您是否也阅读过关于其类型和方法的文档,例如?既然你也在数你可能想用的东西,我会找一个新的家庭教师。
def char_freq_table():
   with open('/home/.....', 'r') as f:
   line = 0
   word = 0
   character = 0
   for i in f:
     line = len(list('f'))
     word += len(i.split()) 
     character =  i.replace(" ", "")  #lose the blank space
   print "line count: ", line
   print "word count: ", word
   print "character count: ", len(character) # length all character

char_freq_table()