counti=0
def removepunc(firstlist):
global counti
try:
for words in firstlist:
cleanstr = re.sub(r'[^\w\s]', ' ', words)
counti += 1
print(counti,":", cleanstr)
Appended_Data.to_excel("test.xlsx", index=False)
return(counti,":", cleanstr)
except:
pass
我在这里试图做的是创建一个函数,从excel工作表中读取一列并删除特殊字符和标点符号,然后将其保存到新的excel工作表中
该列由具有多个句子和特殊字符的字符串组成。我设法删除了那些特殊字符和标点符号,但是,列中有一行是完全空的
当代码到达该行(第506行)时,它会发出一个错误,即代码中需要有一个字符串或字节。我使用了try和except,因此不会显示错误,但代码就到此结束。如何使其跳过(第507行)该行并继续(第508行)运行该函数
你的
try
和except
放错地方了当
try
块中出现错误时停止执行整个块,并调用
except
块这是函数的结尾,因此函数结束并返回
None
要修复此问题,请将
try
块放在cleanstr = re.sub(r'[^\w\s]', ' ', words)
周围,并使用pass
代替continue
,以便控件返回循环中的下一个单词您可以这样做:
相关问题 更多 >
编程相关推荐