使用Python查找给定文本文件的第i个句子中的字符数

时间:2011-06-07 23:41:00

标签: python text-processing

运行时,光标在左侧的命令行上闪烁,没有任何反应。谁能告诉我为什么它没有做到预期的呢?

感谢您的期待。

代码如下:

import sys


i=3   
try:
    text = open(sys.argv[1], 'r')

except IOError:
  print 'Cannot open file %s for reading' % filename

  sys.exit(0)



char = text.read (1)

#navigate to ith sentence, i.e. go to the first letter of the ith sentence

j = 0
for j in range (0, i-1):
    char = text.read (1)
    if char == '.':
        j = j+1

char = text.read(2) #Assuming there is a spce after full-stop/!/?

#count the number of characters in the present sentence

chars = 0

while char != '.' or '!' or '?':
    char = text.read (1)
    chars = chars + 1


print chars

2 个答案:

答案 0 :(得分:4)

我看到一个问题:

while char != '.' or '!' or '?':

应该是:

while char != '.' and char != '!' and char != '?':

...或至少在逻辑上等同的东西,但更清洁,更美味。在前一种情况下,Python将尝试评估char != '.',然后评估'!',并评估'?',两者都被视为true(因为它们不等于零)。所以循环会永远持续下去!

至于:

j = 0
for j in range (0, i-1):
    char = text.read (1)
    if char == '.':
        j = j+1

我认为这不符合你的意图。假设文本文件就像'这是一个句子。这是另一句话。',该循环将char设置为'i',j设置为1.您可能打算使用while循环,如上所述,而不是单个if语句。

答案 1 :(得分:0)

当你到达文件的末尾时,read(1)每次都会返回一个空字符串

    Help on built-in function read:

    read(...)
        read([size]) -> read at most size bytes, returned as a string.

        If the size argument is negative or omitted, read until EOF is reached.
        Notice that when in non-blocking mode, less data than what was requested
        may be returned, even if no size parameter was given.