python 判定数据是否为递增

##背景:需要判定excel文件的某一列是否为递增,如果是的话,还需要修正
xl_r = xlrd.open_workbook("xxx.xls")
# 打开文件并赋值给一个变量
xls_r_sheet = xl_r.sheet_by_index(0)
# 获取第一个sheet表中所有数据
xls_r_col_datas = xls_r_sheet.col_values(2)
# 获取第三列所有数据
new_list = xls_r_col_datas.copy()
# 不能直接使用=,否则指向同一个内存
same_start_index = 0
# 有递增就保持当前相同的第一位
compare_index = 1
# 用于两两比较
for list_index in range(1, len(xls_r_col_datas)):
	first_str  = xls_r_col_datas[same_start_index]
	second_str = xls_r_col_datas[list_index]
	if first_str   != '' and second_str != '' and (first_str  [:-1] == second_str[:-1]) and (first_str[-1]).isdigit() and second_str[-1].isdigit():
	# 判定不为空,且只有最后一位才不同,并且最后一位是数字
		if int(first_str[-1]) + compare_index = int(second_str[-1]):
			print("是递增 %s %s" %(first_str,second_str))
			new_list[list_index] = first_str
			compare_index = compare_index + 1
	#elif first_str   != '' and second_str != '' and (first_str  [:-2] == second_str[:-2]) and (first_str[-2]).isdigit() and second_str[-2].isdigit():
	# 判定不为空,且只有最后两位才不同,并且最后两位是数字,比如a09和a10也算递增
	#elif first_str   != '' and second_str != '' and (first_str  [:-3] == second_str[:-3]) and (first_str[-2]).isdigit() and second_str[-2].isdigit():
	#	判定不为空,且只有最后两位才不同,并且最后两位是数字,比如a099和a100也算递增
	else:
		same_start_index = list_index
		compare_index = 1
			
	

おすすめ

転載: blog.csdn.net/u012700515/article/details/119811818