[关闭]
@wuxin1994 2017-10-08T19:28:28.000000Z 字数 1647 阅读 815

Excel小任务

python


代码如下:

  1. import xlrd
  2. import os
  3. print('------------------------------------------------------------')
  4. file = 'file_in.xlsx'#需要读取的文件
  5. data = xlrd.open_workbook(file) #打开表格文件
  6. sheet = data.sheets()[0] #得到第一个表格
  7. rows = sheet.row_values(0)#得到第一行的关键字
  8. #colume = sheet.col_values
  9. print('------------------------------------------------------------')
  10. print("下面是可查询字段描述:")
  11. row_number = 0
  12. for row in rows:
  13. print(row_number,'--------',row.strip().replace("\r",'').replace('\n',''))
  14. row_number = row_number + 1
  15. print('------------------------------------------------------------')
  16. print("请输入查询的关键字对应的代码(0 ~",row_number-1,"):",end=' ')
  17. while(True):
  18. row_chose = int(input())
  19. if row_chose<= row_number -1 and row_chose >= 0:
  20. break
  21. else:
  22. print("数字有误,请重新输入查询的关键字对应的代码(0 ~",row_number-1,"):",end=' ')
  23. print('------------------------------------------------------------')
  24. my_queues = sheet.col_values(row_chose)
  25. print("查询结果:",'\n',my_queues)
  26. print('------------------------------------------------------------')
  27. print("是否进一步查询?Y/N",end=' : ')
  28. cha = input()
  29. if cha != 'Y' and cha != 'y':
  30. os._exit(0)
  31. print('------------------下面是按照条件查询------------------------------------------')
  32. if row_chose == 0 or row_chose == 1:
  33. flag = True
  34. print("请输入查询的",rows[row_chose],"的名称:",end='')
  35. while(flag):
  36. name = input()
  37. for single in my_queues:
  38. if single == name:
  39. flag = False
  40. index = my_queues.index(name)
  41. print(sheet.row_values(index))
  42. if flag == True:
  43. print("有误!请重新输入查询的",rows[row_chose],"的名称:",end='')
  44. else:
  45. print("请输入查询",rows[row_chose].strip().replace("\r",'').replace('\n',''),"的范围:")
  46. max = float(input("请输入最大值:"))
  47. min = float(input('请输入最小值:'))
  48. if max < min:
  49. max,min = min,max
  50. for single in my_queues:
  51. try:
  52. if float(single) <= max and float(single) >= min:
  53. print(sheet.row_values(my_queues.index(single)))
  54. except:
  55. pass
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注