在python中压缩和解压缩文本文件
问题描述:
我需要修复此程序,以便从解压缩的文件中删除标点符号。例如,当文件原始文本被解压缩时,单词和标点符号之间有空格。在python中压缩和解压缩文本文件
例如:cheese ,
应该返回cheese,
def RemoveSpace(ln): #subroutine used to remove the spaces after the punctuation
line = ""
line2 = ""
puncpst = []
for g in range(1, len(line)):
if line[g] == "." or line[g] == "," or line[g] == "!" or line[g] == "?":
puncpst.append(g) #get the positions of punctuation marks in a list
for b in range(len(line)):
if b + 1 not in puncpst:
line2 = line2 + line[b]
return line2
答
的原因码不起作用是if语句后的压痕。请更正如下压痕:
if b+1 not in puncpst:
line2 = line2+line[b]
另一种方法来处理它是直接替换字符串中的空间:
line.replace(" .",".")
line.replace(" ,",",")
答
这听起来像你的程序应该是这样的:
def RemoveSpace(line):
puncpst = []
for g in range(1, len(line)):
if line[g] == "." or line[g] == "," or line[g] == "!" or line[g] == "?":
puncpst.append(g) #get the positions of punctuation marks in a list
ret = ""
for b in range(len(line)):
if b + 1 not in puncpst:
ret += line[b]
return ret
您原来的def RemoveSpace(ln):
其中ln
未被使用
的改进版本,带头从@ v.coder,可能是这样的:
def RemoveSpace2(line):
punctuation = ['.', ',', '!', '?']
for p in punctuation:
original = ' ' + p
line = line.replace(original, p)
return line
EF RemoveSpace(LN):所使用的标点符号 行之后以删除空格#subroutine = “” LINE2 =” “ puncpst = [] g范围内(1,len(行)): if line [g] ==”。“或行[g] ==“,”或行[g] ==“!”或线[g] ==“?”: puncpst.append(g)#如果b + 1不在puncpst中,请为列中的b获取标点符号列表 中的位置: : line2 = line2 + line [b] return line2 – Manal
您应该确保您的程序格式正确。 Python对此非常关注。除此之外,我没有看到你的程序如何检查标点符号之前是否有空格。 – quamrana