我的脚本清除数组中不需要的字符串,如“@#$!”还有其他的东西。 脚本按预期工作,但当excel行大小较大时,它的速度非常慢
我试图使用numpy,如果它可以加快它,但我不太熟悉的是,所以我可能是用错了
xls = pd.ExcelFile(path)
df = xls.parse("Sheet2")
TeleNum = np.array(df['telephone'].values)
def replace(orignstr): # removes the unwanted string from numbers
for elem in badstr:
if elem in orignstr:
orignstr = orignstr.replace(elem, '')
return orignstr
for UncleanNum in tqdm(TeleNum):
newnum = replace(str(UncleanNum)) # calling replace function
df['telephone'] = df['telephone'].replace(UncleanNum, newnum) # store string back in data frame
我还尝试删除该方法以确定是否有帮助,并将其作为一个代码块放置,但速度保持不变
for UncleanNum in tqdm(TeleNum):
orignstr = str(UncleanNum)
for elem in badstr:
if elem in orignstr:
orignstr = orignstr.replace(elem, '')
print(orignstr)
df['telephone'] = df['telephone'].replace(UncleanNum, orignstr)
TeleNum = np.array(df['telephone'].values)
当前运行200000个excel文件的脚本速度约为70it/s,大约需要一个小时才能完成。这不是很好,因为这只是许多函数的一个函数
我的python还不太高级。我只是学习,因为我脚本,所以如果你有任何指针,它将不胜感激
编辑:
我处理的大多数数组元素都是数字,但有些元素中有字符串。我正在尝试删除数组元素中的所有字符串
例如
FD3459002912
*345*9002912$
如果要从字符串中清除所有不是数字的内容,可以直接使用re.sub,如下所示:
相关问题 更多 >
编程相关推荐