Python 当在句号后拆分文本行时,如何指定在标题';博士和博士;?

Python 当在句号后拆分文本行时,如何指定在标题';博士和博士;?,python,Python,在Python程序中打开.txt文件时,我希望文本的输出格式像句子一样,即在显示句号后生成新行。这就是我到目前为止所取得的成绩,但我不知道如何防止在句末不使用句号时发生这种情况,例如“Dr.”或“I.e.”等。请使用in关键字进行检查 #!/usr/bin/python #Opening the file myFile=open ('Text File.txt', 'r') #Printing the files original text first for line in myFile.

在Python程序中打开.txt文件时,我希望文本的输出格式像句子一样,即在显示句号后生成新行。这就是我到目前为止所取得的成绩,但我不知道如何防止在句末不使用句号时发生这种情况,例如“Dr.”或“I.e.”等。请使用in关键字进行检查

#!/usr/bin/python

#Opening the file
myFile=open ('Text File.txt', 'r')

#Printing the files original text first
for line in myFile.readlines():
print line

#Splitting the text
varLine = line
splitLine = varLine.split (". ") 

#Printing the edited text
print splitLine

#Closing the file
myFile.close()

如果你控制输入,最好的方法是在一句话的末尾使用两个空格(就像人们应该使用的,IMHO),然后在
上使用split这样你就不会碰医生或是

如果你不控制输入。。。我不确定这是否真的是Pythonic,但有一种方法可以做到这一点:使用占位符标识要保存句点的所有位置。下面,我假设
'XYZ'
从未出现在我的文本中。你可以随心所欲地把它复杂化,越复杂越好(不太可能以这种方式遇到它)


为什么会这样?可能是因为它没有回答上面的问题。如果
'Dr.Bob'
拆分行
将是
['Dr','Bob']
'.' in "Dr."
# True

'.' in "Bob"
# False
sentence = "Hello, Dr. Brown.  Nice to meet you.  I'm Bob."
targets = ['Dr.', 'i.e.', 'etc.']
replacements = [t.replace('.', placeholder) for t in targets]
# replacements now looks like: ['DrXYZ', 'iXYZeXYZ', 'etcXYZ']
for i in range(len(targets)):
    sentence = sentence.replace(targets[i], replacements[i])
# sentence now looks like: "Hello, DrXYZ Brown.  Nice to meet you.  I'm Bob."
output = sentence.split('. ')
# output now looks like: ['Hello, DrXYZ Brown', ' Nice to meet you', " I'm Bob."]
output = [o.replace(placeholder, '.') for o in output]
print(output)
>>> ['Hello, Dr. Brown', ' Nice to meet you', " I'm Bob."]