可能重复:
Numbering the sentences inside a <P> in a .xml file?句子拆分的基本编号?
我刚刚开始编程,所以这个问题是很琐碎,除了我。我有一个包含内容就像一个.xml文件:
<p> sentence1. sentence2. sentence3.</p>
<p> sentence1. </p>
现在我已经写有BeautifulSoup脚本追加每个单款有STRING的结尾,所以它看起来像:
<p> sentence1. sentence2. sentence3. STRING</p>
<p> sentence1. STRING </p>
在<只包含一个我想做的句子。但是,如果一个< p>包含比我更多的句子,我想添加STRING到每个句子结尾+句子编号。例如,上段为:
<p> sentence1. STRING1 sentence2. STRING2 sentence3. STRING3 </p>
这里是1句与.append方法我的工作脚本,但我无法得到它的多个句子工作。任何帮助,将不胜感激!
soup = BeautifulSoup(xmlfile)
p = norm.findAll("p")
for i in p:
dotsplit = re.compile(r'\. \w')
sentences = dotsplit.split(i.text)
if len(sentences) == 1:
appendix = "STRING"
i.append(appendix)
print i
if len(sentences) > 1:
for x in sentences:
sentencenumber = ???????
# Should equal (index of sentences)+1, meaning sentences[0] = 1
appendix = sentencenumber + "STRING"
i.append(appendix)
print i
如何从以前的帖子此不同:http://stackoverflow.com/questions/12643798/numbering-the-句子内的ap-in-a-xml文件? –
关于有点不相关的笔记:我不确定'规范'来自哪里。另外,在循环内部使用're.compile'并且每次重新赋值都没什么意义 - 把它放在循环外部,或者使用're.split(r'\。\ w')' - 库将“实习”字符串,并“缓存”正则表达式无论如何... –