python字符串、元组和列表常用的一些方法

来源:互联网 发布:linux vim 无法保存 编辑:程序博客网 时间:2024/05/16 05:08

#coding=UTF-8

#the first method

print "\n"

#寻找字符串中第一个出现可以用find

#寻找由特定间隔符隔开的第一个可以用find

#需找特定间隔符隔开的第几个,先用split分割,然后用下标找

#判断一个元素是否在列表中,可以使用in很简单

file_object =open("c:\\sqltest.txt","r")

sqlquery=["select","show"]

sqlupdate=["insert","update","delete"]

sqldml=["create","drop"]

 

lines = file_object.readlines()

for line in lines:

   sql = line

   n=sql.find(" ")

   sqltype = sql[0:n]

   if sqltype in sqlquery:

       print sqltype+" is a query sql"

   elif sqltype in sqlupdate:

       print sqltype+" is a update sql"

   elif sqltype in sqldml:

       print sqltype+" is a create/drop sql"

   #print sql,

print "\n\nsuccess"

 

file_object.close()

 

字符串、元组和列表常用的几个函数:split、find、index、in、append、分片操作list[m:n],str[m:n]和两者的强制转化list()。遍历时候可以使用for…in…逐个判断。

 

 

 

 

原创粉丝点击