之前学习Python的时候自己写的小例子,过段时间看就不记得是什么意思了,先保存下。
例子1-生成图形:
import math
def 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/python3
import pprint, pickle
file = 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 FirefoxBinary
import time
#import os
browser = webdriver.Firefox() # Get local session of firefox
browser.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 webdriver
import time
browser = webdriver.Chrome("E:\python\selenium\chromedriver.exe")
browser.get("http://www.baidu.com")
time.sleep(5)
browser.close()