import pandas as pd
import os,re
# 只需要替换尺寸文件内容就可以了
df=pd.read_excel(os.path.dirname(os.path.abspath(__file__))+'\尺寸.xlsx')
size=df['尺寸'].tolist()
# 正则表达式的使用
def deal(x):
# 先判断是否为nan,nan为float类型
if type(x)==float:
return x
# 再次判断是否是存在中文,中文不做任何处理
elif len(re.findall('[\u4e00-\u9fa5]+',x))>=1:
return x
# 判断数字匹配是否大于3,大于3不做任何处理
elif len(re.findall(r'\d+',x))>3:
return x
# 判断数字等于3时,进行对数字排序取最后两个进行拼接
elif len(re.findall(r'\d+',x))==3:
output = re.findall(r'\d+', x)
# 做一个排序
output = sorted([int(x) for x in output])
return str(output[1]) + '*' + str(output[2])
# 判断数字等于2时,进行对数字排序取进行拼接
elif len(re.findall(r'\d+',x))==2:
output=re.findall(r'\d+',x)
# 做一个排序
output=sorted([int(x) for x in output])
return str(output[0])+'*'+str(output[1])
# 判断数字等于1时,进行自定义处理
elif len(re.findall(r'\d+',x))==1:
output = re.findall(r'\d+', x)
return '直径'+str(output[0])
# 其他情况不做任何处理
else:
return x
df['尺寸']=[deal(x) for x in size]
df.to_excel(os.path.dirname(os.path.abspath(__file__))+'\尺寸处理后.xlsx',index=False)
对正则处理有疑问的可以和博主交流