python學習之模塊(pip),列表生成式,模塊操做mysql,excel

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 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())
View Code

      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文件,也能夠選擇多個
View Code

      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 本文歡迎轉載,但未經做者贊成必須保留此段聲明,且在文章頁面明顯位置給出原文鏈接,不然視爲侵權。

相關文章
相關標籤/搜索