【发布时间】:2017-03-12 02:50:36
【问题描述】:
from docx import Document
import string
c_dic = ['a','b','c','d','e','f','g','h','i','j','k','l','n','o','p','q','r','s','t','u','v','w','y','z','m']
doc = Document('test3.docx')
firstSen = doc.paragraphs[0].text
indexLetters = 0
while indexLetters < len(c_dic):
d_dic = c_dic[indexLetters]
indexLetters += 1
secondSen = firstSen.replace(d_dic,"")
print (secondSen)
#results:
#hello there
#hllo thr
#hello there
#....
我正在尝试从 text3 文档中删除句子。第一句话是“你好”。我正在尝试运行此脚本并删除该句子。然而,脚本只是删除了句子的一部分,并反复给我“你好”。
*编辑 我计划将此扩展到具有多种外语的单独文档。所以我正在尝试从文档中删除所有英文段落。
【问题讨论】:
-
sencondSen = firstSen.replace(d_dic, "")它将替换来自 firstSen 的 d_dic 字母并将其存储在secondSen但您不会更改fisrtSensefisrtSen将始终保持不变并且循环将运行 26次因为c_dic26 的长度。现在你可以修改你的逻辑
标签: python python-3.x ms-word python-docx