python基础html
生成式python
列表生成式 格式 [表达式 for 表达式 in 迭代对象 (可加判断)]mysql
原:git
1 res1 = [] 2 for i in range(1,5): 3 res1.append(i) 4 print(res1)
改:github
1 res2 = [i for i in range(1,5)] 2 print(res2)
字典生成式 格式 {key:value for 表达式 in 迭代对象 (可加判断)}sql
a = "adasdsasad" b = "asdasdasdg" dict = {x:y for x,y in zip(a,b) if x==y} print(dict)#{'a': 'a', 's': 's'}
集合生成式
ccc = {x for x in range(50) if x <20} print(ccc)#{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19}
生成器推导式 (生成元祖) 一个生成器只能运行一次,只能迭代一次,第二次迭代就没了
t = (x for x in range(10) if x >2) print(t)#<generator object <genexpr> at 0x0000000001E992B0>生成器对象 print(tuple(t))#(3, 4, 5, 6, 7, 8, 9) for i in t: print("再次使用生成器",i)#为空,由于生成器只能运行一次
模块shell
自定义模块数据库
本身写的python代码组成的模块bootstrap
经常使用模块app
python自带的模块
http://www.javashuo.com/article/p-eegngytt-r.html
第三方模块
pip式傻瓜安装:python定义第三方的模块程序/命令
pip list 查看pip全部的安装模块
pip freeze>my_pip.txt 把全部的pip模块导出
pip install -r my_pip.txt 指定文件导入pip
手动安装:对应pip源下载
指定同一python版本:
xxx.tar.gz 下载下来,先解压找到setup.py文件,cmd,执行python setup.py install
xxx.whl 下载下来直接cmd,执行pip install xxxx.whl
指定对不一样python版本安装:
python3.6 -m pip install xxxx.whl
python2.7 m pip install xxxx.whl
pycharm手动安装
pipy上传 将本身写好的程序上传到pipy源上,之后咱们只要pip install本身的源就能够了
1.注册pipy保证可用https://pypi.org/
2.下载须要打包的源:
''' @File : a.py @Copyright : Rainbol @Date : 2019/8/15 @Desc : ''' #!/usr/bin/env python """ Setuptools bootstrapping installer. Maintained at https://github.com/pypa/setuptools/tree/bootstrap. Run this script to install or upgrade setuptools. This method is DEPRECATED. Check https://github.com/pypa/setuptools/issues/581 for more details. """ import os import shutil import sys import tempfile import zipfile import optparse import subprocess import platform import textwrap import contextlib from distutils import log try: from urllib.request import urlopen except ImportError: from urllib2 import urlopen try: from site import USER_SITE except ImportError: USER_SITE = None # 33.1.1 is the last version that supports setuptools self upgrade/installation. DEFAULT_VERSION = "33.1.1" DEFAULT_URL = "https://pypi.io/packages/source/s/setuptools/" DEFAULT_SAVE_DIR = os.curdir DEFAULT_DEPRECATION_MESSAGE = "ez_setup.py is deprecated and when using it setuptools will be pinned to {0} since it's the last version that supports setuptools self upgrade/installation, check https://github.com/pypa/setuptools/issues/581 for more info; use pip to install setuptools" MEANINGFUL_INVALID_ZIP_ERR_MSG = 'Maybe {0} is corrupted, delete it and try again.' log.warn(DEFAULT_DEPRECATION_MESSAGE.format(DEFAULT_VERSION)) def _python_cmd(*args): """ Execute a command. Return True if the command succeeded. """ args = (sys.executable,) + args return subprocess.call(args) == 0 def _install(archive_filename, install_args=()): """Install Setuptools.""" with archive_context(archive_filename): # installing log.warn('Installing Setuptools') if not _python_cmd('setup.py', 'install', *install_args): log.warn('Something went wrong during the installation.') log.warn('See the error message above.') # exitcode will be 2 return 2 def _build_egg(egg, archive_filename, to_dir): """Build Setuptools egg.""" with archive_context(archive_filename): # building an egg log.warn('Building a Setuptools egg in %s', to_dir) _python_cmd('setup.py', '-q', 'bdist_egg', '--dist-dir', to_dir) # returning the result log.warn(egg) if not os.path.exists(egg): raise IOError('Could not build the egg.') class ContextualZipFile(zipfile.ZipFile): """Supplement ZipFile class to support context manager for Python 2.6.""" def __enter__(self): return self def __exit__(self, type, value, traceback): self.close() def __new__(cls, *args, **kwargs): """Construct a ZipFile or ContextualZipFile as appropriate.""" if hasattr(zipfile.ZipFile, '__exit__'): return zipfile.ZipFile(*args, **kwargs) return super(ContextualZipFile, cls).__new__(cls) @contextlib.contextmanager def archive_context(filename): """ Unzip filename to a temporary directory, set to the cwd. The unzipped target is cleaned up after. """ tmpdir = tempfile.mkdtemp() log.warn('Extracting in %s', tmpdir) old_wd = os.getcwd() try: os.chdir(tmpdir) try: with ContextualZipFile(filename) as archive: archive.extractall() except zipfile.BadZipfile as err: if not err.args: err.args = ('', ) err.args = err.args + ( MEANINGFUL_INVALID_ZIP_ERR_MSG.format(filename), ) raise # going in the directory subdir = os.path.join(tmpdir, os.listdir(tmpdir)[0]) os.chdir(subdir) log.warn('Now working in %s', subdir) yield finally: os.chdir(old_wd) shutil.rmtree(tmpdir) def _do_download(version, download_base, to_dir, download_delay): """Download Setuptools.""" py_desig = 'py{sys.version_info[0]}.{sys.version_info[1]}'.format(sys=sys) tp = 'setuptools-{version}-{py_desig}.egg' egg = os.path.join(to_dir, tp.format(**locals())) if not os.path.exists(egg): archive = download_setuptools(version, download_base, to_dir, download_delay) _build_egg(egg, archive, to_dir) sys.path.insert(0, egg) # Remove previously-imported pkg_resources if present (see # https://bitbucket.org/pypa/setuptools/pull-request/7/ for details). if 'pkg_resources' in sys.modules: _unload_pkg_resources() import setuptools setuptools.bootstrap_install_from = egg def use_setuptools( version=DEFAULT_VERSION, download_base=DEFAULT_URL, to_dir=DEFAULT_SAVE_DIR, download_delay=15): """ Ensure that a setuptools version is installed. Return None. Raise SystemExit if the requested version or later cannot be installed. """ to_dir = os.path.abspath(to_dir) # prior to importing, capture the module state for # representative modules. rep_modules = 'pkg_resources', 'setuptools' imported = set(sys.modules).intersection(rep_modules) try: import pkg_resources pkg_resources.require("setuptools>=" + version) # a suitable version is already installed return except ImportError: # pkg_resources not available; setuptools is not installed; download pass except pkg_resources.DistributionNotFound: # no version of setuptools was found; allow download pass except pkg_resources.VersionConflict as VC_err: if imported: _conflict_bail(VC_err, version) # otherwise, unload pkg_resources to allow the downloaded version to # take precedence. del pkg_resources _unload_pkg_resources() return _do_download(version, download_base, to_dir, download_delay) def _conflict_bail(VC_err, version): """ Setuptools was imported prior to invocation, so it is unsafe to unload it. Bail out. """ conflict_tmpl = textwrap.dedent(""" The required version of setuptools (>={version}) is not available, and can't be installed while this script is running. Please install a more recent version first, using 'easy_install -U setuptools'. (Currently using {VC_err.args[0]!r}) """) msg = conflict_tmpl.format(**locals()) sys.stderr.write(msg) sys.exit(2) def _unload_pkg_resources(): sys.meta_path = [ importer for importer in sys.meta_path if importer.__class__.__module__ != 'pkg_resources.extern' ] del_modules = [ name for name in sys.modules if name.startswith('pkg_resources') ] for mod_name in del_modules: del sys.modules[mod_name] def _clean_check(cmd, target): """ Run the command to download target. If the command fails, clean up before re-raising the error. """ try: subprocess.check_call(cmd) except subprocess.CalledProcessError: if os.access(target, os.F_OK): os.unlink(target) raise def download_file_powershell(url, target): """ Download the file at url to target using Powershell. Powershell will validate trust. Raise an exception if the command cannot complete. """ target = os.path.abspath(target) ps_cmd = ( "[System.Net.WebRequest]::DefaultWebProxy.Credentials = " "[System.Net.CredentialCache]::DefaultCredentials; " '(new-object System.Net.WebClient).DownloadFile("%(url)s", "%(target)s")' % locals() ) cmd = [ 'powershell', '-Command', ps_cmd, ] _clean_check(cmd, target) def has_powershell(): """Determine if Powershell is available.""" if platform.system() != 'Windows': return False cmd = ['powershell', '-Command', 'echo test'] with open(os.path.devnull, 'wb') as devnull: try: subprocess.check_call(cmd, stdout=devnull, stderr=devnull) except Exception: return False return True download_file_powershell.viable = has_powershell def download_file_curl(url, target): cmd = ['curl', url, '--location', '--silent', '--output', target] _clean_check(cmd, target) def has_curl(): cmd = ['curl', '--version'] with open(os.path.devnull, 'wb') as devnull: try: subprocess.check_call(cmd, stdout=devnull, stderr=devnull) except Exception: return False return True download_file_curl.viable = has_curl def download_file_wget(url, target): cmd = ['wget', url, '--quiet', '--output-document', target] _clean_check(cmd, target) def has_wget(): cmd = ['wget', '--version'] with open(os.path.devnull, 'wb') as devnull: try: subprocess.check_call(cmd, stdout=devnull, stderr=devnull) except Exception: return False return True download_file_wget.viable = has_wget def download_file_insecure(url, target): """Use Python to download the file, without connection authentication.""" src = urlopen(url) try: # Read all the data in one block. data = src.read() finally: src.close() # Write all the data in one block to avoid creating a partial file. with open(target, "wb") as dst: dst.write(data) download_file_insecure.viable = lambda: True def get_best_downloader(): downloaders = ( download_file_powershell, download_file_curl, download_file_wget, download_file_insecure, ) viable_downloaders = (dl for dl in downloaders if dl.viable()) return next(viable_downloaders, None) def download_setuptools( version=DEFAULT_VERSION, download_base=DEFAULT_URL, to_dir=DEFAULT_SAVE_DIR, delay=15, downloader_factory=get_best_downloader): """ Download setuptools from a specified location and return its filename. `version` should be a valid setuptools version number that is available as an sdist for download under the `download_base` URL (which should end with a '/'). `to_dir` is the directory where the egg will be downloaded. `delay` is the number of seconds to pause before an actual download attempt. ``downloader_factory`` should be a function taking no arguments and returning a function for downloading a URL to a target. """ # making sure we use the absolute path to_dir = os.path.abspath(to_dir) zip_name = "setuptools-%s.zip" % version url = download_base + zip_name saveto = os.path.join(to_dir, zip_name) if not os.path.exists(saveto): # Avoid repeated downloads log.warn("Downloading %s", url) downloader = downloader_factory() downloader(url, saveto) return os.path.realpath(saveto) def _build_install_args(options): """ Build the arguments to 'python setup.py install' on the setuptools package. Returns list of command line arguments. """ return ['--user'] if options.user_install else [] def _parse_args(): """Parse the command line for options.""" parser = optparse.OptionParser() parser.add_option( '--user', dest='user_install', action='store_true', default=False, help='install in user site package') parser.add_option( '--download-base', dest='download_base', metavar="URL", default=DEFAULT_URL, help='alternative URL from where to download the setuptools package') parser.add_option( '--insecure', dest='downloader_factory', action='store_const', const=lambda: download_file_insecure, default=get_best_downloader, help='Use internal, non-validating downloader' ) parser.add_option( '--version', help="Specify which version to download", default=DEFAULT_VERSION, ) parser.add_option( '--to-dir', help="Directory to save (and re-use) package", default=DEFAULT_SAVE_DIR, ) options, args = parser.parse_args() # positional arguments are ignored return options def _download_args(options): """Return args for download_setuptools function from cmdline args.""" return dict( version=options.version, download_base=options.download_base, downloader_factory=options.downloader_factory, to_dir=options.to_dir, ) def main(): """Install or upgrade setuptools and EasyInstall.""" options = _parse_args() archive = download_setuptools(**_download_args(options)) return _install(archive, _build_install_args(options)) if __name__ == '__main__': sys.exit(main())
3.将步骤2 的代码放到一个py文件并执行
4.文件整理,将须要打包的文件放到一个文件统一文件夹中,里面要空的__init__.py文件
5.打包:在文件夹的同级目录再创建一个setup.py文件,里面写入代码,并执行python setup.py sdist,会生成一个dist文件和名字.egg-info文件
from setuptools import setup setup( name='你的项目源名称', #xxx version='项目版本',#1.0 description='项目的描述', #xxx author='做者',#rainbol author_email='xxx@qq.com', #邮箱 url='https://www.cnblogs.com/RainBol/', #相应的地址 py_modules=['rainbol.test']) #要打包的文件目录,我这里放在rainbol文件的test.py文件,也能够选择多个
6.pip install twine执行
7.上传命令 twine upload dist/* 输入pipy帐号密码
8.查看
xpinyin模块 把中文变成中文拼音
import xpinyin s= xpinyin.Pinyin() print(s.get_pinyin("饕餮",""))
python操做mysql数据库模块
pip install pymysql
import pymysql conn = pymysql.connect(host="192.168.1.1", password="123456", user="root", db="nihao", port=3306, charset="utf8", autocommit=True) cur = conn.cursor() # 创建游标,理解成取件员,cur = conn.cursor(cursor=pymysql.cursors.DictCursor)参数cursor表示选择字典游标类型
#循环游标 取到数据库的每行数据 劣势:占用大量内存,优点:减小每次链接mysql交互频率
for i in cur:
print(i)
# 查 cur.execute('show tables;') # 执行sql语句不会返回结果 print(cur.fetchall()) # 获取数据库中的结果,保存在二维元祖中 print(cur.fetchone()) # 获取一条数据,若是数据返回多条,直取第一条,返回一个一维元祖 fetchmany(n)表示想拿几条填几条 print(cur.description) # 获取表结构信息 # 增,删,改 sql = cur.execute('insert into xxx VALUES xxx') #单条单个
sql = cur.execute('insert into class(id,username,password) values(%s,%s,%s),('1','rainbol','123456')) #以传参的形式传给execute,单个参数不须要元祖形式,多个参数必须元祖形式,实现单条多个
list = (
('1','rainbol1','123456'),
('2','rainbol2','12345678')
('3','rainbol3','12345687')
)
sql = cur.executemany('insert into class(id,username,password) values(%s,%s,%s)',list) #executemany会自动把上面list列表中的参数循环传递,实现多条多个的状况
cur.execute(sql)
new_id = cur1.lastrowid #若是查询的表中有自增列,那这一行就能够获取最新的那个自增列的值,若是插入的是多条数据,那lastrowid取的是最后一个
sql = 'insert into xxx(k1,k2,k3,...)values(v1,v2,v3,...)'#很明显这样字段一多就要写死了,因此如下方法实现长字段插入
deal_sql = 'insert into xxx(%s)values(%s)'
key_list = []
value_list = []
for k, v in data.items():
key_list.append(k)
value_list.append('%%(%s)s' % k)
sql = sql % (','.join(key_list), ','.join(value_list))
cur.execut(deal_sql,data)
call_id1 = cur.callproc('p1') #callproc执行存储过程,无参数:pymysql调用mysql存储过程,p为存储过程函数名
cur.callproc('p2',args=(a,'123','b','asdf')) #有参数:pymysql调用mysql存储过程,获取存储过程的结果集并将返回值设置给@_p2_....;带参数写在一个元祖变量中,args中填写的是实参
#pymysql内部操做mysql
#set @_p2_0 = a
#set @_p2_1 = 123
#set @_p2_2 = b
@set @_p2_3 = asdf
res = cur.fetchall()#获取存储过程结果集,这里res没有返回mysql存储过程的返回值,可是经过上面的内部操做执行给了mysql,经过下面方式查询就能够拿到返回值
cur.execute('select @_p1_0,@_p1_1,@_p1_2,@_p1_3') #查询参数的执行返回值 @_为固定格式,p1为函数名,_1为参数序号,有几个就写几个
# conn.commit() # 提交数据,若是前面定义了autocommit=True,那就不用了 cur.close() # 关闭游标 conn.close() # 关闭链接
封装函数
def my_db(ip, user, password, db, sql, port=3306, charset='utf8'): conn = pymysql.connect(host=ip, password=password, user=user, db=db, port=port, charset=charset, autocommit=True) cur = conn.cursor() cur.execute(sql) res = cur.fetchall() cur.close() conn.close() return res
python操做excel
pip install xlwt pip install xlrd pip install xlutils
新增写excel:
import xlrd, xlutils, xlwt book = xlwt.Workbook() # 建立excel文件,能够指定路径,默认当前路径 sheet = book.add_sheet('sheet1') # 增长excel页,能够指定名称 sheet.write(0, 0, 'username') # 指定行和列,规律,当前行行不变,当前列列不变 sheet.write(1, 0, 'password') sheet.write(0, 1, 'admin') sheet.write(1, 1, '123456') book.save('世界你好.xls') # 保存文件并输出文件名,xlsx会打不开,wps能够打开
大批量数据循环嵌套
import xlwt all_shuzu = [ [1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3] ] book = xlwt.Workbook() sheet = book.add_sheet('sheet1') line = 0 for shuzu in all_shuzu: col = 0 for s in shuzu: sheet.write(line,col,s) col += 1 line += 1 book.save('世界你好.xls')
读excel:xlrd模块只能读不能写
import xlrd book = xlrd.open_workbook('世界你好.xls') sheet = book.sheet_by_index(0) # 根据编号找sheet1 # sheet = book.sheet_by_name("sheet1")#根据名字找sheet1 print(sheet.nrows) # 获取这个sheet1有多少行 print(sheet.ncols) # 获取这个sheet1有多少列 print(sheet.cell(0, 0).value) #获取到这个sheet1指定单元格内容 print(sheet.row_values(0)) #获取到这个sheet1指定整行的内容,返回list print(sheet.col_values(0)) #获取到这个sheet1指定整列的内容,返回list
修改excel:只能写,因此要和xlrd配合使用
import xlrd from xlutils import copy book = xlrd.open_workbook('世界你好.xls') # 读文件 new_book = copy.copy(book) # 复制文件句柄并放到一个新的文件中 sheet = new_book.get_sheet(0) # 获取sheet页面 sheet.write(0, 0, "我很差") new_book.save("世界你好.xls")
版权声明:本文原创发表于 博客园,做者为 RainBol 本文欢迎转载,但未经做者赞成必须保留此段声明,且在文章页面明显位置给出原文链接,不然视为侵权。