Find the number of characters in a file using Python

后端 未结 16 1707
春和景丽
春和景丽 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.

提交回复
热议问题