python:选房抽签小工具
1、房间号和姓名写在house_name.xls的house标签页中【注意!名字均不要改动】
2、运行house.py
3、当前同目录下会生成result.xls,即为结果;程序运行过程中不要打开该文件,运行完成后再打开,否则结果无法写入
4、若要重新生成,重新运行house.py即可,结果会重新生成
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os, shutil, random, sys, json
from os.path import join
from xlrd import open_workbook
from xlutils.copy import copy default_encoding = 'utf-8'
if sys.getdefaultencoding() != default_encoding:
stdi, stdo, stde = sys.stdin, sys.stdout, sys.stderr
reload(sys)
sys.setdefaultencoding(default_encoding)
sys.stdin, sys.stdout, sys.stderr = stdi, stdo, stde url_excel_path = 'house_name.xls'
result_excel_path = 'result.xls'
sheet_name = 'house' class house:
def read_excel(self, sheet_name):
workbook = open_workbook(url_excel_path)
# 读取sheet1表的内容
sheet1_content = workbook.sheet_by_name(sheet_name)
# 获取sheet1表中的行数
row_count = sheet1_content.nrows
# print row_count
# 读取第1列的内容
all_house = sheet1_content.col_values(0)
all_house = json.dumps(all_house)
# 读取第2列的内容
all_name = sheet1_content.col_values(1)
return all_name def Write_Excel(self, sheetName, rowIndex, lineIndex, content):
"""
- rowIndex:行
- lineIndex:列
"""
if not os.path.exists(result_excel_path):
print "%s not exists" % result_excel_path
shutil.copy(url_excel_path, result_excel_path)
rowIndex = int(rowIndex)
lineIndex = int(lineIndex)
rb = 'r+w'
rb = open_workbook(result_excel_path, 'r')
rbook = open_workbook(result_excel_path, 'w')
wb = copy(rbook)
sheetIndex = rbook.sheet_names().index(sheetName)
wb.get_sheet(int(sheetIndex)).write(int(rowIndex), int(lineIndex), content)
wb.save(result_excel_path) def random_name(self, sheet_name):
# houselist = self.read_excel(sheet_name)[0]
namelist = self.read_excel(sheet_name)
print "name:%s" % namelist
# 打乱姓名
random.shuffle(namelist)
print "new name:%s" % namelist
length = len(namelist)
for i in xrange(length):
print i
print namelist[i]
self.Write_Excel(sheet_name, i, 1, namelist[i]) def check_then_create(self, file_path):
"""
检查文件夹是否存在,不存在则自动创建
"""
isExists = os.path.exists(file_path)
if not isExists:
os.makedirs(file_path) if __name__ == '__main__':
obj = house()
# obj.read_excel(sheet_name)
obj.random_name(sheet_name)
python:选房抽签小工具的更多相关文章
- python开发目录合并小工具 PathMerge
前言 这个程序陆陆续续开发了几天,正好我在学Python,就一边做一边学,倒是学到不少东西. 不得不说python是快速开发的好工具. 程序做了一些改进,这两天又忙着毕设,现在才想起来发到博客上.想想 ...
- python 3.6 MJ小工具
2017.07.14 update 做了个界面,不需要使用cmd命令行+文件路径的方式来使用了: 链接如下: http://www.cnblogs.com/chenyuebai/p/7150382.h ...
- 目不识丁的我使用Python编写汉字注音小工具
一万点暴击伤害 人懒起来太可怕了,放了个十一充分激发了我的惰性.然后公众号就这么停了半个月,好惭愧- 新学期儿子的幼儿园上线了APP,每天作业通过app布置后,家长需要陪着孩子学习,并上传视频才算完成 ...
- python转exe的小工具
其实只是在cxfreeze的基础上加了个壳,做成窗口软件了 使用了pyqt做的界面,软件发布在了开源中国上,可以直接去下面的地址查看 http://git.oschina.net/robocky/py ...
- Python: tkinter实例改名小工具
#!/usr/bin/env python #coding=utf-8 # # 版权所有 2014 yao_yu (http://blog.csdn.net/yao_yu_126) # 本代码以MIT ...
- 入坑python 自己写的小工具,纪念一下
这个程序的功能是可以从表格中读取某一列数据,传到IDs 这一个参数里,然后在url中获取相应的请求值,并打印 import urllib.request import json import xlrd ...
- python一些实用的小工具
1 搭一个简易的本地局域网 python -m http.server 2 获取当前目录下的所有文件名 3 进度条效果 import sys,time for i in range(50): sy ...
- python之字符聊天小工具
server side: # coding: gb2312#socket server端#获取socket构造及常量from socket import *#''代表服务器为localhostmyHo ...
- 几个可以提高工作效率的Python内置小工具
在这篇文章里,我们将会介绍4个Python解释器自身提供的小工具.这些小工具在笔者的日常工作中经常用到,减少了各种时间的浪费,然而,却很容易被大家忽略.每当有新来的同事看到我这么使用时,都忍不住感叹, ...
随机推荐
- 对python的super方法的用法理解
参考链接:https://www.cnblogs.com/dkblog/archive/2011/02/24/1980654.html https://www.cnblogs.com/wjx1/p/5 ...
- leetcode python 006
## 改为z型字符串def change_to_z(s,n): ## 字符串不能生成完整的区,用空格补全 b=len(s)%(2*n-2) if b!=0: s+=' ...
- like contains
like : %/% = *****/**** contains: abc = 字符串中含有 abc
- oracle 11g安装过程
1,工具:Oracle_win64_11gR2_database(64位oracle 11g数据库).iso安装文件,win10系统 2,右键,解压后,点击setup.exe,系统会检测本机的环境,如 ...
- vim中将小写替换为大写--快速解决变量名风格
将C语言的下划线分割快速替换为Java的驼峰方式. 命令如下 :%s/_\([a-zA-Z]\)/\U\1/g 参考文档
- 缓慢拒绝服务攻击- slowloris.pl
How use Slowloris Requirements: # sudo apt-get update # sudo apt-get install perl # sudo apt-get i ...
- 学习笔记TF045:人工智能、深度学习、TensorFlow、比赛、公司
人工智能,用计算机实现人类智能.机器通过大量训练数据训练,程序不断自我学习.修正训练模型.模型本质,一堆参数,描述业务特点.机器学习和深度学习(结合深度神经网络). 传统计算机器下棋,贪婪算法,Alp ...
- python trie
Trie 库 https://github.com/pytries/marisa-trie/blob/master/docs/tutorial.rst http://marisa-trie.readt ...
- 如何将项目连接数据库(连接mysql)
首先需要在项目中加入这一串代码: //加载驱动类 连接数据库有多种方式 比如:jdbc 桥接 Connection con=null; try { Class.forName("com.my ...
- 推荐一些关于学习Html Css和Js的书吗?
前端易学易懂,随着移动互联网的日益兴起,it行业对于前端的需求也在不断的提高,那么从前端小白修炼成为前端大神的这个过程之中,一些必备的枕边书也是必不可少的. 第一本,入门<Head first ...