Python小例子
来源:互联网 发布:glide源码原理 编辑:程序博客网 时间:2024/06/13 17:37
之前学习Python的时候自己写的小例子,过段时间看就不记得是什么意思了,先保存下。
例子1-生成图形:
import mathdef fourth(n): n+=1 cc=math.ceil((n*2-19)/2) ff=math.floor((n*2-19)/2) print(cc,ff) for col in range(n*2-1): if(col==0 or col==n*2-2): print('+',end='') else: print('-',end='') print() for a in reversed(range(n-1)): print('|',end='') for b in range(a): print(' ',end='') for c in range(n-a-1): if(c==n-a-2 or a==0): print('*',end='') else: print('-',end='') for d in range(n-a-2): if(a==0): print('*',end='') else: print('+',end='') for e in range(a): print(' ',end='') print('|') for i in range(n-2): print('|',end='') for j in range(i+1): print(' ',end='') for k in reversed(range(n-i-2)): if(k==0): print('*',end='') else: print('+',end='') for l in reversed(range(n-i-3)): print('-',end='') for m in range(i+1): print(' ',end='') print('|') for col in range(n*2-1): if(col==0 or col==n*2-2): print('+',end='') else: print('-',end='') print() print('|',end='') for i in range(cc): print(' ',end='') print("This is a graph.",end='') for i in range(ff): print(' ',end='') print('|') for col in range(n*2-1): if(col==0 or col==n*2-2): print('+',end='') else: print('-',end='') fourth(20)
例子2-去除符号\n\r\t及空格:
#!/usr/bin/python3import pprint, picklefile = open('project.html', 'r')filewrite = open('project-w.html', 'w')nextline = file.readline()findnum = nextline.find(r'\n')while nextline!='': while (r'\n' in nextline): findnum = nextline.find(r'\n') nexttext = nextline[0:findnum] #print(r'\r' in nexttext) #print(r'\t' in nexttext) if (r'\r' in nexttext or r'\t' in nexttext): nexttext = nexttext.replace(r"\r","") nexttext = nexttext.replace(r"\t","") if(nexttext=="" or nexttext.isspace()): pass else: filewrite.write(nexttext+'\n') #filewrite.write(nexttext+'\n') nextline = nextline[findnum+2:] nextline = file.readline()file.close()filewrite.close()
例子3-selenium-Firefox:
from selenium import webdriver#from selenium.common.exceptions import NoSuchElementException#from selenium.webdriver.common.keys import Keys#from selenium.webdriver.firefox.firefox_binary import FirefoxBinaryimport time#import osbrowser = webdriver.Firefox() # Get local session of firefoxbrowser.get("http://www.baidu.com") # Load page#assert "baidu" in browser.title#elem = browser.find_element_by_name("p") # Find the query box#elem.send_keys("seleniumhq" + Keys.RETURN)time.sleep(5) # Let the page load, will be added to the API#try:# browser.find_element_by_xpath("//a[contains(@href,'http://seleniumhq.org')]")#except NoSuchElementException:# assert 0, "can't find seleniumhq"browser.close()#gecko = os.path.normpath(os.path.join(os.path.dirname(__file__), 'geckodriver'))#binary = FirefoxBinary(r'C:\Program Files (x86)\Mozilla Firefox\firefox.exe')#browser = webdriver.Firefox(firefox_binary=binary, executable_path=gecko+'.exe')#browser.get("http://www.baidu.com")#browser.close()
例子4-selenium-chrome:
from selenium import webdriverimport timebrowser = webdriver.Chrome("E:\python\selenium\chromedriver.exe")browser.get("http://www.baidu.com")time.sleep(5)browser.close()
阅读全文
0 0
- python应用小例子
- python 小例子
- Python 语法小例子
- Python小例子
- python zipfile小例子
- Python多线程小例子
- python socket 小例子
- python的一个小例子
- python网络编程小例子
- python网络编程小例子
- python 3 UDP小例子
- Python future模块小例子
- Python-学习-小例子练习
- 文件处理python小例子
- python矩阵操作小例子
- python的练习小例子
- Python读取XML配置文件小例子
- BeautifulSoup Python抓网页小例子
- 从远程服务器下载文件
- Python day01————一只小野怪夜袭Python大陆
- ORB_SLAM2+kinect2.0+Ubuntu14.04+ROS Indigo总结
- 机器学习(4)-应用机器学习的建议_Advice for Applying Machine Learning
- TextView获取每行的内容
- Python小例子
- 1.判断两个链表是否相交,若相交,求交点。(假设链表不带环)2.判断两个链表是否相交,若相交,求交点。(假设链表可能带环)【升级版】
- notes2016
- kill -9 和 kill -15 的区别
- Spring Boot + Gradle 依赖管理
- Caffe关闭日志输出
- Linux 学习总结 unit01访问命令行
- redis缓存在linux上搭建的过程
- 使用eclipse工程导出jar包