您的位置:首页 > 编程语言 > Python开发

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

2012-02-29 15:29 417 查看
#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…逐个判断。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: