Find the number of characters in a file using Python

后端 未结 16 1316
春和景丽
春和景丽 2021-02-07 00:34

Here is the question:

I have a file with these words:

hey how are you
I am fine and you
Yes I am fine

And it is asked to find the numbe

相关标签:
16条回答
  • 2021-02-07 01:26

    This is too long for a comment.

    Python 2 or 3? Because it really matters. Try out the following in your REPL for both:

    Python 2.7.12
    >>>len("taña")
    5
    
    Python 3.5.2
    >>>len("taña")
    4
    

    Huh? The answer lies in unicode. That ñ is an 'n' with a combining diacritical. Meaning its 1 character, but not 1 byte. So unless you're working with plain ASCII text, you'd better specify which version of python your character counting function is for.

    0 讨论(0)
  • 2021-02-07 01:26
    num_lines = sum(1 for line in open('filename.txt'))
    num_words = sum(1 for word in open('filename.txt').read().split())
    num_chars = sum(len(word) for word in open('filename.txt').read().split())
    
    0 讨论(0)
  • 2021-02-07 01:27

    Remember that each line (except for the last) has a line separator. I.e. "\r\n" for Windows or "\n" for Linux and Mac.

    Thus, exactly two characters are added in this case, as 47 and not 45.

    A nice way to overcome this could be to use:

    import os
    
    fname=input("enter the name of the file:")
    infile=open(fname, 'r')
    lines=0
    words=0
    characters=0
    for line in infile:
        line = line.strip(os.linesep)
        wordslist=line.split()
        lines=lines+1
        words=words+len(wordslist)
        characters=characters+ len(line)
    print(lines)
    print(words)
    print(characters)
    
    0 讨论(0)
  • 2021-02-07 01:30

    To count the characters, you should count each individual word. So you could have another loop that counts characters:

    for word in wordslist:
        characters += len(word)
    

    That ought to do it. The wordslist should probably take away newline characters on the right, something like wordslist = line.rstrip().split() perhaps.

    0 讨论(0)
提交回复
热议问题