显示django版本信息
import django
print ( django. get_version( ) )
字符串换行符也在字符的匹配中,以下输出:texxx
user_confirm = "confirm\n"
if user_confirm == "confirm" :
print ( "test" )
else :
print ( "texxx" )
存入数据库的数据中存有字符\的正确输入及数据库操作,格式使用了mb4,通用性更强一些,可以存放一些图标。
import mysql. connector
conn = mysql. connector. connect( user= 'root' , password= '123456' , host= 'localhost' , port= '3306' ,
database= 'bank' , charset= 'utf8mb4' )
c = conn. cursor( )
c. execute( "select answer,rules from bank.pro_bank where problem = 'Age(inyears)';" )
res = c. fetchall( )
print ( res)
print ( res[ 0 ] [ 0 ] )
str_ = res[ 0 ] [ 0 ]
print ( str_)
str_. replace( "\\" , "\\\\" )
print ( str_)
res[ 0 ] [ 0 ] . replace( "\\" , "\\\\" )
print ( res[ 0 ] [ 0 ] )
print ( res)
conn. commit( )
c. close( )
conn. close( )
随机取列表中的数值
import random
list1 = [ '#QID4-4-label span' , '#QID4-7-label span' , '#QID4-5-label span' , '#QID4-3-label span' , '#QID4-2-label span' , '#QID4-8-label span' ]
a = random. sample( list1, 1 )
b = random. sample( list1, 3 )
print ( a)
print ( b)
random. shuffle( list1)
print ( list1)
自动化测试中,是否关闭了浏览器的测试
from selenium import webdriver
object_existed = False
driver = webdriver. Chrome( )
if driver is not None :
try :
time. sleep( 6 )
print ( "test" )
driver. execute_script( 'javascript:void(0);' )
print ( "执行11" )
object_existed = True
except :
try :
print ( "关闭驱动" )
driver. quit( )
finally :
driver = None
finally :
pass
if not object_existed:
print ( "浏览器已关闭或标签页已关闭" )
if object_existed:
print ( "浏览器还没有关闭" )
try :
driver. get( "https://www.baidu.com" )
except :
print ( "不能请求网页了" )
while循环每次列表都是重新赋值,资源都会释放,要特别注意指针的内存泄漏
i = 3
while ( i) :
str = list ( )
print ( len ( str ) )
print ( id ( str ) )
if "lint" in str :
print ( "true" )
else :
print ( "false" )
str . append( "lint" )
str . append( "caredte" )
print ( len ( str ) )
print ( str )
i -= 1
if "lint" in str :
print ( "true" )
字符串的存储在常量区,重复使用还是使用的之前的内存地址
name = 'admin'
name1 = name
name2 = name
print ( id ( name) , name)
print ( id ( name1) , name1)
print ( id ( name2) , name2)
print ( "\n" )
name = 'test'
print ( id ( name) , name)
print ( id ( name1) , name1)
print ( id ( name2) , name2)
print ( "\n" )
name = 'admin'
print ( id ( name) , name)
print ( id ( name1) , name1)
print ( id ( name2) , name2)
更换一段字符串中特定位置的连续数字,一下模板为更新最后一个数字,输出:[‘19’, ‘12’] fasd19-18-dggfas
import re
def rreplace ( self, old, new, * max ) :
count = len ( self)
if max and str ( max [ 0 ] ) . isdigit( ) :
count = max [ 0 ]
return new. join( self. rsplit( old, count) )
str_ = 'fasd19-12-dggfas'
plt = 6
buf = re. findall( '\d+' , str_)
print ( buf)
buff = int ( buf[ len ( buf) - 1 ] ) + plt
print ( rreplace( str_, buf[ len ( buf) - 1 ] , str ( buff) , 1 ) )
python语音提示信息’
import pyttsx3
pt = pyttsx3. init( )
rate = pt. getProperty( 'rate' )
pt. setProperty( 'rate' , 120 )
pt. setProperty( 'volume' , 0.7 )
pt. say( "j3hc53t" + "页面有填空题,页面需要人工操作,操作完成后点击确认按钮" )
pt. runAndWait( )
import win32com. client as win
speak = win. Dispatch( "SAPI.SpVoice" )
speak. Speak( "Come on" )
speak. Speak( "7号浏览器需要人工操作。" )
python使用isalnum()去除特殊字符,以下输出:我select前面中文符号lawtopenglishsymbollast
strsql = "我select!(前面中文符号)🌱🌏law🌏🌱top(english symbol last)@#$%^';"
strsql = '' . join( char for char in strsql if char. isalnum( ) )
print ( strsql)
判断第一个字符是不是数字
str_title = "66test55 tttt"
if str_title[ 0 ] . isdigit( ) == True :
str_title = str_title[ str_title. find( " " ) + 1 : ]
print ( str_title)
去除字符中所有的非数字:50
import numpy as np
strsql = " £50 "
print ( re. sub( u"([^\u0030-\u0039])" , "" , strsql) )
产生随机数:第一个不包含6,第二个不包含0。
i = 20
while i:
num = np. random. randint( 2 , 6 )
print ( num)
i = i- 1
print ( np. random. randint( - 10 , 0 ) )
循环次数:前两句会直接是0。后两句向前循环,循环到0位置。
for i in range ( - 5 ) :
print ( i)
for i in range ( 5 , 0 , - 1 ) :
print ( i)
不保留小数的除法:print(5//2) 遍历一串字符的方法:
strs = 'tangcorleone'
for ch in iter ( strs) :
print ( ch)
time. sleep( 0.2 )
python的replace()是按照顺序替换的,输出:!Thisquestionrequiresananswer.QuestionTitletangcorleone
str_title = "!Thisques tionrequire sananswer.Ques tionTitletangcorleone"
str_title = str_title. replace( "!Thisquestionrequiresananswer." , "" ) . replace( "QuestionTitle" , "" ) . replace( "\n" , "" ) . replace( " " , "" )
print ( str_title)
```