在python中压缩和解压缩文本文件
我需要修复这个程序,以便它从解压缩文件中删除标点符号。例如,解压缩文件原始文本时,单词和标点符号之间有一个空格 示例:在python中压缩和解压缩文本文件,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)):
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(" ,",",")
代码无法工作的原因是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 RemoveSpace2(line):
punctuation = ['.', ',', '!', '?']
for p in punctuation:
original = ' ' + p
line = line.replace(original, p)
return line
您原来的haddef RemoveSpace(ln):
其中未使用ln
由@v.coder牵头的改进版本可能如下:
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 RemoveSpace2(line):
punctuation = ['.', ',', '!', '?']
for p in punctuation:
original = ' ' + p
line = line.replace(original, p)
return line
听起来你的程序应该是这样的:
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 RemoveSpace2(line):
punctuation = ['.', ',', '!', '?']
for p in punctuation:
original = ' ' + p
line = line.replace(original, p)
return line
您原来的haddef RemoveSpace(ln):
其中未使用ln
由@v.coder牵头的改进版本可能如下:
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 RemoveSpace2(line):
punctuation = ['.', ',', '!', '?']
for p in punctuation:
original = ' ' + p
line = line.replace(original, p)
return line
ef RemoveSpace(ln):#用于移除范围内g(1,len(line))的标点符号行=”“line2=”“puncpst=[]后的空格的子例程:如果行[g]=”,或行[g]=”,“或行[g]=”!“或行[g]=”?”:puncpst.append(g)#获取范围内b(len)列表中标点符号的位置(line)):如果b+1不在puncpst:line2=line2+line[b]return line2中,您应该确保您的程序格式正确。Python对此非常挑剔。此外,我不知道您的程序如何检查标点符号前是否确实有空格。ef RemoveSpace(ln):#用于删除范围内g(1,len(line))的标点符号行=“line2=”“puncpst=[]后的空格的子例程:如果行[g]=”,或行[g]=”,“或行[g]=”!“或行[g]=”?”:puncpst.append(g)#获取范围内b(len(line))列表中标点符号的位置:如果b+1不在puncpst:line2=line2+line[b]return line2中,您应该确保您的程序格式正确。Python对此非常挑剔。此外,我不知道您的程序如何检查标点符号前是否确实有空格。