dra*_*ons 0 python if-statement docstring python-3.x
让我通过我的确切代码:这是短模块
class SentenceSplitter:
def __init__(self, filename=None):
self._raw_text = self.raw_text(filename)
self._sentences = self.to_sentences()
def raw_text(self, filename):
text = ''
with open(filename, 'r') as file:
for line in file.readlines():
line = line.strip()
text += ''.join(line.replace(line, line+' '))
file.close()
text = text.strip() # Deal with the last whitespace
return text
def to_sentences(self):
""" Sentence boundaries occur at '.', '!', '?' except that,
there are some not-sentence boundaries that
may occur before/after the period.
"""
raw_text = self._raw_text
sentences = []
sentence = ''
boundary = None
for char in raw_text:
sentence += ''.join(char)
if char == '!' or char == '?':
sentences.append(sentence)
sentence = ''
""" The sign -> refers to 'followed by' """
elif char == '.':
i = raw_text.index(char) # slicing previous/following characters
boundary = True
if boundary:
sentences.append(sentence)
sentence = ''
return sentences
Run Code Online (Sandbox Code Playgroud)
主要:
import textchange
ss = textchange.SentenceSplitter(filename='text.txt')
print(ss._sentences)
Run Code Online (Sandbox Code Playgroud)
第一个if语句之后的docstring
""" The sign -> refers to 'followed by' """
Run Code Online (Sandbox Code Playgroud)
我评论了它,程序运行,否则没有.elif语句中有更多代码,但在确定它仍然抛出错误后将其删除.这是追溯:
Traceback (most recent call last):
File "D:\Programs\Python 3.3.2\Tutorials\46 Simple Python Exercises.py", line 26, in
<module>
import textchange
File "D:\Programs\Python 3.3.2\Tutorials\textchange.py", line 51
elif char == '.':
^
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)
文档字符串只是在函数开头找到的字符串文字.他们仍然必须遵守缩进规则.
您的字符串未正确缩进elif
块; 通过被从去凹陷if
块之前,你结束了if
- elif
- else
完全块,没有elif
被允许跟随.
使用常规的正常注释,一行开头#
; 仅包含注释的行不受缩进规则的限制:
if char == '!' or char == '?':
sentences.append(sentence)
sentence = ''
# The sign -> refers to 'followed by'
elif char == '.':
i = raw_text.index(char) # slicing previous/following characters
boundary = True
Run Code Online (Sandbox Code Playgroud)
或者缩进字符串(它完全由Python作为代码执行,但是没有分配,因此再次丢弃):
if char == '!' or char == '?':
sentences.append(sentence)
sentence = ''
elif char == '.':
""" The sign -> refers to 'followed by' """
i = raw_text.index(char) # slicing previous/following characters
boundary = True
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
257 次 |
最近记录: |