设为首页 收藏本站
查看: 892|回复: 0

[经验分享] 我的python基础总结

[复制链接]

尚未签到

发表于 2018-8-5 06:40:30 | 显示全部楼层 |阅读模式
  1.mac下给python3安装requests库和scrapy库
  https://blog.csdn.net/u011413922/article/details/78026866
  pip3 install requests
  pip3 install Scrapy #注意S大写
  两种安装方式 https://blog.csdn.net/cighao/article/details/47860041
  最新安装步骤:https://blog.csdn.net/qq_36305327/article/details/60144614
  2.重启
  https://blog.csdn.net/wen_1108/article/details/78682578
  3.最新版的下载地址
  http://www.pc6.com/mac/170572.html
  requests:https://pypi.org/project/requests/。 安装方式多样。。。
  4.mac版本的切换
  https://blog.csdn.net/blue_zy/article/details/69568240
  pycharm切换python版本:https://blog.csdn.net/sgfmby1994/article/details/77876873
  5.基础知识
  1>中文编码
  1>#!/usr/bin/python
  # -*- coding: UTF-8 -*-
  print "你好,世界";
  同时idea File Encoding需要设置
  2>交互式编程  脚本式编程
  独有的标识符和保留字
  多行语句\   注释#   多引号、单引号、三引号的使用    空行  python -h
  3>运算符、条件表达式、循环表达式、break、continue、pass
  4>变量类型:字符串、Number、原祖、字典(json)、日期时间、列表(数组)
  5>函数
  def 函数名(参数列表):
  函数体
  在 python 中,strings, tuples, 和 numbers 是不可更改的对象,而 list,dict 等则是可以修改的对象。   list、dict参数->指针
  默认参数:def printinfo( name, age = 35 ):
  不定长参数:
  #!/usr/bin/python3
  # 可写函数说明
  def printinfo( arg1, *vartuple ):
  "打印任何传入的参数"
  print ("输出: ")
  print (arg1)
  for var in vartuple:
  print (var)
  return
  # 调用printinfo 函数
  printinfo( 10 )
  printinfo( 70, 60, 50 )
  匿名函数:lambda [arg1 [,arg2,.....argn]]:expression
  变量的作用域:L--->E--->G--->B  Python 中只有模块(module),类(class)以及函数(def、lambda)才会引入新的作用域,其它的代码块(如 if/elif/else/、try/except、for/while等)是不会引入新的作用域的
  global 和 nonlocal关键字:
  num = 1
  def fun1():
  global num  # 需要使用 global 关键字声明
  print(num)
  num = 123
  print(num)
  fun1()
  print(num)
  def outer():
  num = 10
  def inner():
  nonlocal num   # nonlocal关键字声明
  num = 100
  print(num)
  inner()
  print(num)
  outer()
  6>模块
  7>文件io操作。file操作文件open seek  tell read write等方法,os操作目录
  8>支持对异常的处理
  9>时间的处理
  获取时间戳:ticks = time.time()
  时间元组:time.struct_time(tm_year=2018, tm_mon=5, tm_mday=7, tm_hour=10, tm_min=19, tm_sec=50, tm_wday=0, tm_yday=127, tm_isdst=0)
  日期的转换:
  print (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
  print (time.strftime("%a %b %d %H:%M:%S %Y", time.localtime()))
  print (time.mktime(time.strptime(a,"%a %b %d %H:%M:%S %Y")))
  元祖tuple:tup1 = (50,)        tup1 = ('Google', 'Runoob', 1997, 2000);
  访问元祖:print ("tup1[0]: ", tup1[0])    print ("tup2[1:5]: ", tup2[1:5])
  元组元素不可修改,但是可以进组合
  # 以下修改元组元素操作是非法的。
  # tup1[0] = 100
  # 创建一个新的元组
  tup3 = tup1 + tup2;
  print (tup3)
  删除元组:del tup;
  元祖运算符:len  +   *(复制)   in   for in
  10>保留字
  ['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']
  11>import
  将整个模块(somemodule)导入,格式为: import somemodule
  从某个模块中导入多个函数,格式为: from somemodule import firstfunc, secondfunc, thirdfunc
  将某个模块中的全部函数导入,格式为: from somemodule import *
  12>迭代器和生成器
  迭代器:
  list=[1,2,3,4]
  it = iter(list)    # 创建迭代器对象
  for x in it:
  print (x, end=" ")
  生成器:自定义的迭代器
  import sys
  def fibonacci(n): # 生成器函数 - 斐波那契
  a, b, counter = 0, 1, 0
  while True:
  if (counter > n):
  return
  yield a
  a, b = b, a + b
  counter += 1
  f = fibonacci(10) # f 是一个迭代器,由生成器返回生成
  while True:
  try:
  print (next(f), end=" ")
  except StopIteration:
  sys.exit()
  6.操作mysql
  面向对象、可以创建一个类
  1>连接mysql数据库    PyMysql(python3)
  安装PyMysql,链接mysql的库
  import pymysql
  db = pymysql.connect("localhost","root","root","jiqi" )
  cursor = db.cursor()
  cursor.execute("SELECT VERSION()")
  data = cursor.fetchone()
  print ("Database version : %s " % data)
  db.close()
  2>创建表:
  # 打开数据库连接
  db = pymysql.connect("localhost","testuser","test123","TESTDB" )
  # 使用 cursor() 方法创建一个游标对象 cursor
  cursor = db.cursor()
  # 使用 execute() 方法执行 SQL,如果表存在则删除
  cursor.execute("DROP TABLE IF EXISTS EMPLOYEE")
  # 使用预处理语句创建表
  sql = """CREATE TABLE EMPLOYEE (
  FIRST_NAME  CHAR(20) NOT NULL,
  LAST_NAME  CHAR(20),
  AGE INT,
  SEX CHAR(1),
  INCOME FLOAT )"""
  cursor.execute(sql)
  # 关闭数据库连接
  db.close()
  3>数据库插入操作:
  import pymysql
  # 打开数据库连接
  db = pymysql.connect("localhost","testuser","test123","TESTDB" )
  # 使用cursor()方法获取操作游标
  cursor = db.cursor()
  # SQL 插入语句
  sql = """INSERT INTO EMPLOYEE(FIRST_NAME,
  LAST_NAME, AGE, SEX, INCOME)
  VALUES ('Mac', 'Mohan', 20, 'M', 2000)"""
  try:
  # 执行sql语句
  cursor.execute(sql)
  # 提交到数据库执行
  db.commit()
  except:
  # 如果发生错误则回滚
  db.rollback()
  # 关闭数据库连接
  db.close()
  4>数据查询操作:
  # 打开数据库连接
  db = pymysql.connect("localhost","testuser","test123","TESTDB" )
  # 使用cursor()方法获取操作游标
  cursor = db.cursor()
  # SQL 查询语句
  sql = "SELECT * FROM EMPLOYEE \
  WHERE INCOME > '%d'" % (1000)
  try:
  # 执行SQL语句
  cursor.execute(sql)
  # 获取所有记录列表
  results = cursor.fetchall()
  for row in results:
  fname = row[0]
  lname = row[1]
  age = row[2]
  sex = row[3]
  income = row[4]
  # 打印结果
  print ("fname=%s,lname=%s,age=%d,sex=%s,income=%d" % \
  (fname, lname, age, sex, income ))
  except:
  print ("Error: unable to fetch data")
  # 关闭数据库连接
  db.close()
  5>数据库更新操作:
  import pymysql
  # 打开数据库连接
  db = pymysql.connect("localhost","testuser","test123","TESTDB" )
  # 使用cursor()方法获取操作游标
  cursor = db.cursor()
  # SQL 更新语句
  sql = "UPDATE EMPLOYEE SET AGE = AGE + 1 WHERE SEX = '%c'" % ('M')
  try:
  # 执行SQL语句
  cursor.execute(sql)
  # 提交到数据库执行
  db.commit()
  except:
  # 发生错误时回滚
  db.rollback()
  # 关闭数据库连接
  db.close()
  6>删除操纵
  import pymysql
  # 打开数据库连接
  db = pymysql.connect("localhost","testuser","test123","TESTDB" )
  # 使用cursor()方法获取操作游标
  cursor = db.cursor()
  # SQL 删除语句
  sql = "DELETE FROM EMPLOYEE WHERE AGE > '%d'" % (20)
  try:
  # 执行SQL语句
  cursor.execute(sql)
  # 提交修改
  db.commit()
  except:
  # 发生错误时回滚
  db.rollback()
  # 关闭连接
  db.close()
  7.网络编程
  服务器端口:
  # 导入 socket、sys 模块
  import socket
  import sys
  # 创建 socket 对象
  s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  # 获取本地主机名
  host = socket.gethostname()
  # 设置端口好
  port = 9999
  # 连接服务,指定主机和端口
  s.connect((host, port))
  # 接收小于 1024 字节的数据
  msg = s.recv(1024)
  s.close()
  print (msg.decode('utf-8'))
  客户端端口:
  # 导入 socket、sys 模块
  import socket
  import sys
  # 创建 socket 对象
  serversocket = socket.socket(
  socket.AF_INET, socket.SOCK_STREAM)
  # 获取本地主机名
  host = socket.gethostname()
  port = 9999
  # 绑定端口号
  serversocket.bind((host, port))
  # 设置最大连接数,超过后排队
  serversocket.listen(5)
  while True:
  # 建立客户端连接
  clientsocket, addr = serversocket.accept()
  print("连接地址: %s" % str(addr))
  msg = '欢迎访问菜鸟教程!' + "\r\n"
  clientsocket.send(msg.encode('utf-8'))
  clientsocket.close()
  8.多线程
  创建多线程
  import _thread
  import time
  # 为线程定义一个函数
  def print_time( threadName, delay):
  count = 0
  while count < 5:
  time.sleep(delay)
  count += 1
  print (&quot;%s: %s&quot; % ( threadName, time.ctime(time.time()) ))
  # 创建两个线程
  try:
  _thread.start_new_thread( print_time, (&quot;Thread-1&quot;, 2, ) )
  _thread.start_new_thread( print_time, (&quot;Thread-2&quot;, 4, ) )
  except:
  print (&quot;Error: 无法启动线程&quot;)
  while 1:
  pass
  使用 threading 模块创建线程
  import threading
  import time
  exitFlag = 0
  class myThread (threading.Thread):
  def __init__(self, threadID, name, counter):
  threading.Thread.__init__(self)
  self.threadID = threadID
  self.name = name
  self.counter = counter
  def run(self):
  print (&quot;开始线程:&quot; + self.name)
  print_time(self.name, self.counter, 5)
  print (&quot;退出线程:&quot; + self.name)
  def print_time(threadName, delay, counter):
  while counter:
  if exitFlag:
  threadName.exit()
  time.sleep(delay)
  print (&quot;%s: %s&quot; % (threadName, time.ctime(time.time())))
  counter -= 1
  # 创建新线程
  thread1 = myThread(1, &quot;Thread-1&quot;, 1)
  thread2 = myThread(2, &quot;Thread-2&quot;, 2)
  # 开启新线程
  thread1.start()
  thread2.start()
  thread1.join()
  thread2.join()
  print (&quot;退出主线程&quot;)
  线程的同步:
  class myThread (threading.Thread):
  def __init__(self, threadID, name, counter):
  threading.Thread.__init__(self)
  self.threadID = threadID
  self.name = name
  self.counter = counter
  def run(self):
  print (&quot;开启线程: &quot; + self.name)
  # 获取锁,用于线程同步
  threadLock.acquire()
  print_time(self.name, self.counter, 3)
  # 释放锁,开启下一个线程
  threadLock.release()
  def print_time(threadName, delay, counter):
  while counter:
  time.sleep(delay)
  print (&quot;%s: %s&quot; % (threadName, time.ctime(time.time())))
  counter -= 1
  threadLock = threading.Lock()
  threads = []
  # 创建新线程
  thread1 = myThread(1, &quot;Thread-1&quot;, 1)
  thread2 = myThread(2, &quot;Thread-2&quot;, 2)
  # 开启新线程
  thread1.start()
  thread2.start()
  # 添加线程到线程列表
  threads.append(thread1)
  threads.append(thread2)
  # 等待所有线程完成
  for t in threads:
  t.join()
  print (&quot;退出主线程&quot;)
  线程的优先级队列:
  import queue
  import threading
  import time
  exitFlag = 0
  class myThread (threading.Thread):
  def __init__(self, threadID, name, q):
  threading.Thread.__init__(self)
  self.threadID = threadID
  self.name = name
  self.q = q
  def run(self):
  print (&quot;开启线程:&quot; + self.name)
  process_data(self.name, self.q)
  print (&quot;退出线程:&quot; + self.name)
  def process_data(threadName, q):
  while not exitFlag:
  queueLock.acquire()
  if not workQueue.empty():
  data = q.get()
  queueLock.release()
  print (&quot;%s processing %s&quot; % (threadName, data))
  else:
  queueLock.release()
  time.sleep(1)
  threadList = [&quot;Thread-1&quot;, &quot;Thread-2&quot;, &quot;Thread-3&quot;]
  nameList = [&quot;One&quot;, &quot;Two&quot;, &quot;Three&quot;, &quot;Four&quot;, &quot;Five&quot;]
  queueLock = threading.Lock()
  workQueue = queue.Queue(10)
  threads = []
  threadID = 1
  # 创建新线程
  for tName in threadList:
  thread = myThread(threadID, tName, workQueue)
  thread.start()
  threads.append(thread)
  threadID += 1
  # 填充队列
  queueLock.acquire()
  for word in nameList:
  workQueue.put(word)
  queueLock.release()
  # 等待队列清空
  while not workQueue.empty():
  pass
  # 通知线程是时候退出
  exitFlag = 1
  # 等待所有线程完成
  for t in threads:
  t.join()
  print (&quot;退出主线程&quot;)
  9.xml解析
  dom解析
  #!/usr/bin/python3
  from xml.dom.minidom import parse
  import xml.dom.minidom
  # 使用minidom解析器打开 XML 文档
  DOMTree = xml.dom.minidom.parse(&quot;movies.xml&quot;)
  collection = DOMTree.documentElement
  if collection.hasAttribute(&quot;shelf&quot;):
  print (&quot;Root element : %s&quot; % collection.getAttribute(&quot;shelf&quot;))
  # 在集合中获取所有电影
  movies = collection.getElementsByTagName(&quot;movie&quot;)
  # 打印每部电影的详细信息
  for movie in movies:
  print (&quot;*****Movie*****&quot;)
  if movie.hasAttribute(&quot;title&quot;):
  print (&quot;Title: %s&quot; % movie.getAttribute(&quot;title&quot;))
  type = movie.getElementsByTagName('type')[0]
  print (&quot;Type: %s&quot; % type.childNodes[0].data)
  format = movie.getElementsByTagName('format')[0]
  print (&quot;Format: %s&quot; % format.childNodes[0].data)
  rating = movie.getElementsByTagName('rating')[0]
  print (&quot;Rating: %s&quot; % rating.childNodes[0].data)
  description = movie.getElementsByTagName('description')[0]
  print (&quot;Description: %s&quot; % description.childNodes[0].data)
  sax解析:
  #!/usr/bin/python3
  import xml.sax
  class MovieHandler( xml.sax.ContentHandler ):
  def __init__(self):
  self.CurrentData = &quot;&quot;
  self.type = &quot;&quot;
  self.format = &quot;&quot;
  self.year = &quot;&quot;
  self.rating = &quot;&quot;
  self.stars = &quot;&quot;
  self.description = &quot;&quot;
  # 元素开始调用
  def startElement(self, tag, attributes):
  self.CurrentData = tag
  if tag == &quot;movie&quot;:
  print (&quot;*****Movie*****&quot;)
  title = attributes[&quot;title&quot;]

  print (&quot;Title:&quot;,>  # 元素结束调用
  def endElement(self, tag):
  if self.CurrentData == &quot;type&quot;:
  print (&quot;Type:&quot;, self.type)
  elif self.CurrentData == &quot;format&quot;:
  print (&quot;Format:&quot;, self.format)
  elif self.CurrentData == &quot;year&quot;:
  print (&quot;Year:&quot;, self.year)
  elif self.CurrentData == &quot;rating&quot;:
  print (&quot;Rating:&quot;, self.rating)
  elif self.CurrentData == &quot;stars&quot;:
  print (&quot;Stars:&quot;, self.stars)
  elif self.CurrentData == &quot;description&quot;:
  print (&quot;Description:&quot;, self.description)
  self.CurrentData = &quot;&quot;
  # 读取字符时调用
  def characters(self, content):
  if self.CurrentData == &quot;type&quot;:
  self.type = content
  elif self.CurrentData == &quot;format&quot;:
  self.format = content
  elif self.CurrentData == &quot;year&quot;:
  self.year = content
  elif self.CurrentData == &quot;rating&quot;:
  self.rating = content
  elif self.CurrentData == &quot;stars&quot;:
  self.stars = content
  elif self.CurrentData == &quot;description&quot;:
  self.description = content
  if ( __name__ == &quot;__main__&quot;):
  # 创建一个 XMLReader
  parser = xml.sax.make_parser()
  # 关闭命名空间
  parser.setFeature(xml.sax.handler.feature_namespaces, 0)
  # 重写 ContextHandler
  Handler = MovieHandler()
  parser.setContentHandler( Handler )
  parser.parse(&quot;movies.xml&quot;)
  10.cgi编程:
  处理请求:
  #!/usr/bin/python3
  # CGI处理模块
  import cgi, cgitb
  # 创建 FieldStorage 的实例化
  form = cgi.FieldStorage()
  # 获取数据
  site_name = form.getvalue('name')
  site_url  = form.getvalue('url')
  print (&quot;Content-type:text/html&quot;)
  print ()
  print (&quot;<html>&quot;)
  print (&quot;<head>&quot;)
  print (&quot;<meta charset=\&quot;utf-8\&quot;>&quot;)
  print (&quot;<title>菜鸟教程 CGI 测试实例</title>&quot;)
  print (&quot;</head>&quot;)
  print (&quot;<body>&quot;)
  print (&quot;<h2>%s官网:%s</h2>&quot; % (site_name, site_url))
  print (&quot;</body>&quot;)
  print (&quot;</html>&quot;)

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-546677-1-1.html 上篇帖子: Python Redis-DreamScape 下篇帖子: Python的简介
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表