{"id":2687,"date":"2023-09-06T18:02:48","date_gmt":"2023-09-06T10:02:48","guid":{"rendered":"https:\/\/www.tra56.com\/?p=2687"},"modified":"2023-09-06T18:02:48","modified_gmt":"2023-09-06T10:02:48","slug":"2687","status":"publish","type":"post","link":"https:\/\/www.tra56.com\/2687.html","title":{"rendered":"Python\u7edf\u8ba1\u65f6\u95f4\u5185\u7684\u5e76\u53d1\u6570\u4ee3\u7801\u5b9e\u4f8b"},"content":{"rendered":"

Python\u5b9e\u73b0\u5e76\u53d1\u7684\u624b\u6bb5\uff1a<\/p>\n

1\u3001\u64cd\u4f5c\u7cfb\u7edf\u63d0\u4f9b\uff1a\u8fdb\u7a0b\u3001\u7ebf\u7a0b\uff1b<\/p>\n

2\u3001\u7f16\u7a0b\u8bed\u8a00\u63d0\u4f9b\uff1a\u534f\u7a0b\uff1a\u7528\u6237\u7a7a\u95f4\u7684\u8c03\u5ea6\uff08py3\uff09\uff1b<\/p>\n

# coding:utf-8\r\n\r\n# 1.\u5bfc\u5165\u6a21\u5757\r\n# datatime\u6a21\u5757\u7528\u4e8e\u5b9a\u4e49\u65f6\u95f4\u53ca\u65f6\u95f4\u7684\u52a0\u51cf\u64cd\u4f5c\r\n# MySQLdb\u6a21\u5757\u7528\u4e8ePython2.0\u8fde\u63a5\u6570\u636e\u5e93\uff0cPython3.0\u8fde\u63a5\u6570\u636e\u5e93\u4f7f\u7528pymysql\r\n# xlwt\u6a21\u5757\u662fexcel\u64cd\u4f5c\u6a21\u5757\uff0c\u7528\u4e8e\u5c06\u6570\u636e\u5199\u5165excel\u4e2d\r\n\r\nimport datetime\r\nimport MySQLdb\r\nimport xlwt\r\n\r\n# 2.\u8fde\u63a5\u6570\u636e\u5e93\uff0c\u83b7\u53d6\u6570\u636e\r\n# MySQLdb.connect\u7528\u4e8e\u5b9a\u4e49\u8fde\u63a5\u6570\u636e\u5e93\u7684\u5c5e\u6027\r\n# myconn.cursor()\u5b9a\u4e49\u6e38\u6807\u5bf9\u8c61\r\n# query_sql\u5b9a\u4e49\u67e5\u8be2\u7684\u8bed\u53e5\r\n# mycursor.execute()\u6267\u884c\u67e5\u8be2\u8bed\u53e5\uff0c\u4ec5\u4ec5\u662f\u6267\u884c\u8bed\u53e5\uff0c\u4e0d\u8f93\u51fa\u7ed3\u679c\u3002\r\n# mycursor.fetchall()\u63d0\u53d6\u67e5\u8be2\u6570\u636e\u3002all\u5168\u90e8\u6570\u636e\uff0cone\u5355\u6761\u6570\u636e\uff0cmany\u53d6\u591a\u5c11\u6761\u6570\u636e\u3002fetchmany(10)\u53d610\u6761\u6570\u636e\u3002\r\n# mycursor.close()\u5173\u95ed\u6e38\u6807\r\n# myconn.close()\u5173\u95ed\u8fde\u63a5\r\n\r\nmyconn = MySQLdb.connect(host='1',user='wn',passwd='9eu',db='bs',charset='utf8')\r\nmycursor = myconn.cursor()\r\nquery_sql = '''\r\nselect JOIN_TIME,LEAVE_TIME from commfee where JOIN_TIME between '2019-12-24 15:00:00' and '2019-12-24 15:30:00' \r\n'''\r\n\r\nmycursor.execute(query_sql)\r\nsql_result = mycursor.fetchall()\r\nmycursor.close()\r\nmyconn.close()\r\n\r\n# 3.\u5b9a\u4e49\u5168\u5c40\u53c2\u6570\r\n# sum1 = []\u5b9a\u4e49\u5217\u8868sum1,sum1\u7528\u4e8e\u751f\u6210\u6bd4\u8f83\u7684\u65f6\u95f4\u5217\u8868\r\n# sum2 = []\u5b9a\u4e49\u5217\u8868sum2,sum2\u7528\u4e8e\u751f\u6210\u5e76\u53d1\u6570\u7684\u5217\u8868\r\n\r\nsum1 = []\r\nsum2 = []\r\n\r\n# 4.\u5b9a\u4e49\u6570\u636e\u7b5b\u9009\u51fd\u6570\r\n# compare_time \u6bd4\u8f83\u65f6\u95f4\uff0c\u6700\u5f00\u59cb\u503c\u53d6\u5f00\u59cb\u65f6\u95f4\u7684\u7b2c\u4e00\u4e2a\u503c\u3002\r\n# start_time = [sql_result[i][0] for i in range(0,len(sql_result))]\u5c06\u67e5\u8be2\u5230\u7684\u7ed3\u679c\u62c6\u5206\u4e3a\u4e24\u4e2a\u5217\u8868start_time\u548cend_time\u3002\r\n# compare_time < start_time[len(sql_result)-1]\uff0ccompare_time\u65f6\u95f4\u548cstart_time\u5217\u8868\u4e2d\u7684\u65f6\u95f4\u6bd4\u8f83\r\n# compare_time += datetime.timedelta(seconds=1)\uff0c\u6bcf\u6b21\u6bd4\u8f83\u540e\uff0ccompare_time\u65f6\u95f4+1\r\n# datetime.timedelta(seconds=1)\uff0ctimedelta(seconds=1)\u65f6\u95f4\u53d8\u53161s\r\n# sum1.append(compare_time),\u5c06\u5f97\u5230\u7684compare_time\u5199\u5165sum1\u5217\u8868\u4e2d\u3002\r\n\r\ndef query_data():\r\n  compare_time = sql_result[0][0]\r\n  start_time = [sql_result[i][0] for i in range(0,len(sql_result))]   \r\n  end_time = [sql_result[i][1] for i in range(0,len(sql_result))]\r\n  while compare_time < start_time[len(sql_result)-1]:\r\n     compare_time += datetime.timedelta(seconds=1)     \r\n     count1 = 0     \r\n     count2 = 0\r\n     for time1 in start_time:      \r\n       if time1 <= compare_time:         \r\n         count1 = count1 + 1         \r\n     for time2 in end_time:              \r\n       if time2 <= compare_time:          \r\n         count2 = count2 - 1         \r\n     sum1.append(compare_time)     \r\n     sum2.append(count1+count2)\r\n\r\n# 5.\u5b9a\u4e49excel\u64cd\u4f5c\u51fd\u6570\r\n# xlwt.Workbook(encoding='utf-8')\u5b9a\u4e49\u7f16\u7801\u683c\u5f0f\r\n# wbk.add_sheet('My worksheet')\u5b9a\u4e49\u64cd\u4f5c\u7684sheet\u8868\r\n# xlwt.XFStyle()\u5b9a\u4e49\u5355\u5143\u683c\u683c\u5f0f\r\n# datastyle.num_format_str = 'yyyy-mm-dd hh:mm:ss'\u5b9a\u4e49\u5355\u5143\u683c\u4e2d\u6570\u636e\u683c\u5f0f\r\n# worksheet.write(row,0,sum1[row],datastyle) \u6309\u5b9a\u4e49\u7684\u683c\u5f0f\u5199\u5165\u6570\u636e\r\n# wbk.save()\u4fdd\u5b58\u64cd\u4f5c\u7684excel\u8868\u683c\u3002\r\n\r\ndef re_sheet():      \r\n  wbk = xlwt.Workbook(encoding='utf-8')\r\n  worksheet = wbk.add_sheet('My worksheet')\r\n  datastyle = xlwt.XFStyle()\r\n  datastyle.num_format_str = 'yyyy-mm-dd hh:mm:ss'\r\n  for row in range(0,len(sum1)):    \r\n     worksheet.write(row,0,sum1[row],datastyle)     \r\n     worksheet.write(row,1,sum2[row])     \r\n  wbk.save('Concurrency.xls')  \r\nquery_data()   \r\nre_sheet()<\/pre>\n

<\/p>\n","protected":false},"excerpt":{"rendered":"

Python\u5b9e\u73b0\u5e76\u53d1\u7684\u624b\u6bb5\uff1a 1\u3001\u64cd\u4f5c\u7cfb\u7edf\u63d0\u4f9b\uff1a\u8fdb\u7a0b\u3001\u7ebf\u7a0b\uff1b 2\u3001\u7f16\u7a0b\u8bed\u8a00\u63d0\u4f9b\uff1a\u534f\u7a0b\uff1a\u7528\u6237\u7a7a\u95f4\u7684\u8c03\u5ea6\uff08py3\uff09 […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15],"tags":[],"class_list":["post-2687","post","type-post","status-publish","format-standard","hentry","category-python3"],"_links":{"self":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts\/2687"}],"collection":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/comments?post=2687"}],"version-history":[{"count":1,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts\/2687\/revisions"}],"predecessor-version":[{"id":2688,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts\/2687\/revisions\/2688"}],"wp:attachment":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/media?parent=2687"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/categories?post=2687"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/tags?post=2687"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}