VII Python(6)基础知识(re正则表达式、MySQLdb模块)

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介:

正则表达式REregular expression)是一种小型的、高度专业化的编程语言,肉嵌在python中,通过re模块实现;

当发现有问题用正则表达式可解决时,于是将面临两个问题;

RE模式被编译成一系统的字节码,再由匹配引擎(C写的)执行,RE语言相对小型和受限(功能有限,并非所有字符串处理都能用RE完成);

re模块提供了顶级函数调用,常用的有:findall()sub()match()search()subn()split()

 

In [1]: import re

In [2]: re.<TAB>

re.DEBUG        re.L            re.S            re.U            re.compile      re.findall      re.search       re.sub

re.DOTALL       re.LOCALE       re.Scanner      re.UNICODE      re.copy_reg     re.finditer     re.split        re.subn

re.I            re.M            re.T            re.VERBOSE      re.error        re.match        re.sre_compile  re.sys

re.IGNORECASE   re.MULTILINE    re.TEMPLATE     re.X            re.escape       re.purge        re.sre_parse    re.template

In [2]: help(re.search)   #search()方法用于在字符串中搜索模式第一次匹配的位置,若有匹配到返回的是对象的内存地址,若没匹配到则返回空)

search(pattern, string, flags=0)

In [3]: help(re.findall)   #findall()将匹配到的字符串打包为列表)

findall(pattern, string,flags=0)

   Return a list of all non-overlapping matches in the string.

In [4]: help(re.compile)

compile(pattern, flags=0)

   Compile a regular expression pattern, returning a pattern object.

In [5]: help(re.finditer)

finditer(pattern, string,flags=0)

   Return an iterator over all non-overlapping matches in the

   string.  For each match, theiterator returns a match object.

In [6]: help(re.subn)

subn(pattern, repl,string, count=0, flags=0)

   Return a 2-tuple containing (new_string, number).

In [7]: help(re.match)

match(pattern, string,flags=0)

   Try to apply the pattern at the start of the string, returning

    amatch object, or None if no match was found.

 

正则中默认是打开大小写敏感的,可在标志位处使用re.IGNORECASE取消;

正则中不区分数字,全是字符;

正则默认是贪婪模式,在条件符合时尽可能多的匹配,可在.+后加上?进入非贪婪模式即最小匹配范围;

字符匹配(普通字符和元字符):

普通字符(大多数字符一般都会和自身匹配,例如r'test'会和字符串'test'完全匹配);

元字符;

 

元字符(11个):

. ^  $  * +  ?  {} []  \  |  ()

.点(匹配换行符以外的任意字符);

^(匹配行首,字符串的开始位置,除非设置标志位re.MULTILINE,在re.MULTILINE模式里也可匹配字符串中的每个换行);

$(匹配行尾,行尾被定义为要么是字符串尾,要么是一个换行字符后的任何位置);

*(指定前一个字符匹配0次或任意次,贪婪模式,匹配引擎会尝试重复尽可能多的次数,不超过整数界定范围20亿);

+(前一个字符匹配一次或任意次,默认贪婪模式,若要用非贪婪模式(最小匹配模式)+后跟?);

?(匹配前一个字符0次或1次,可理解为它用于标识某事物是可选的);

{m,n}mn是十进制整数,前一个字符至少m次重复,最多n次;忽略m则下边界是0,忽略n则上边界是无穷大(20亿));

[](常用来指定一个字符集,如[abc][a-z],元字符在字符集中不起作用;字符集中开始处使用^,补集匹配不在区间内的字符);

\num\后跟不同的字符表示不同的意义;若当作转义符用可取消元字符本身的意义;\d等同于[0-9]\D等同于[^0-9]\s等同于[\t\n\r\f\v]\S等同于[^\t\n\r\f\v]\w等同于[a-zA-Z0-9_]\W等同于[^a-zA-Z0-9_]

()(分组)

 

注:

*等同于{0,}+等同于{1,}?等同于{0,1};尽量使用*+?这三个符号,因为既美观而且更关键的是正则引擎内部会对这三个符号进行优化,进而执行效率要高,而不要用等同的{0,}{1,}{0,1}

*+区别(*匹配的前一个字符可以是0次,+要至少是一次);

[]中的元字符代表其本身,不具备元字符的功能;

 

\序号(若序号为单个数字,则是引用序号对应的子组所匹配的字符串,子组的序号从1开始计算;若序号以0开头或是3个字符长度时,那么不会被用于对应的子组,而是用于匹配八进制的数字所表示的ASCII表对应的字符);

\A(等同于^,脱字符);

\Z(等同于$

\b(匹配单词边界,单词被定义为Unicode的字母数字或下划线,注意下划线也是单词的一部分);

\B(与\b相反,匹配非单词边界,例如py\B匹配pythonpy2py3而不匹配py py.py!);

\d(对于Unicode(str类型)模式,匹配任何一个数字,包括[0-9]和其它数字字符,若开启了re.ASCII标志,只匹配[0-9];对于8位模式(bytes类型)匹配[0-9]中任何一个数字);

\D(与\d相反,匹配任何非Unicode的数字,若开启了re.ASCII标志,则相当于匹配[^0-9]);

\s(对于Unicode(str类型)模式,匹配Unicode中的空白字符,包括[\t\n\r\f\v]及其它空白字符,若开启了re.ASCII标志,只匹配[\t\n\r\f\v];对于8位模式(bytes类型),匹配ASCII中定义的空白字符,即[\t\n\r\f\v]);

\S(与\s相反,匹配任何非Unicode中的空白字符,若开启了re.ASCII标志,则相当于匹配[^\t\n\f\r\v]

\w(对于Unicode(str类型)模式,匹配任何以Unicode的单词字符,基本上所有语言字符都可匹配,也包括数字下划线[a-zA-Z0-9],若开启了re.ASCII标志则只匹配[a-zA-Z0-9];对于8位模式(bytes类型),匹配ASCII中定义的字母数字);

\W(与\w相反,匹配任何非Unicode的单词字符,若开启了re.ASCII标志,则相当于[^a-zA-Z0-9]);

 

转义符的特殊情况:

\a \b  \f  \n \r  \t  \u \U  \v  \x  \\

\b(通常用于单词边界,只有在字符类中才表示backspace);

\u\U(只在Unicode模式下才会被识别);

\num(八进制转义是有限制的,如果第1个数字是0,或如果有3个八进制数字,那么就被认为是八进制;其它情况则被认为是子组引用;至于字符串,八进制转义最多只能是3个数字的长度);

 

flags标志位:

ASCII(简写A,使得转义符\w\b\d\s只能匹配ASCII字符);

DOTALL(简写S,使得.点匹配任何符号,包括换行符,无敌状态);

IGNORECASE(简写I,匹配时不区分大小写);

LOCALE(简写L,支持当前的语言设置);

MULTILINE(简写M,多行匹配(要查找的内容为多行),影响^$);

VERBOSE(简写X,启用详细的正则表达式,正则的书写为多行时);

 

编译正则表达式:

re模块提供了一个正则表达式引擎的接口,可将REstring编译成对象并用它们进行匹配;

如果要重复的使用某个正则表达式,可将正则表达式编译成模式对象,使用re.compile()来编译;

若只需查找或匹配一次使用模块级别的方法,如re.findall(r'[A-Z]','string');若要多次使用正则表达式,在循环情况下,就要先编译再进行调用,这样使用更方便;

In [6]: help(re.compile)

compile(pattern, flags=0)

   Compile a regular expression pattern, returning a pattern object.

In [2]: p=re.compile(r'[A-Z]')

In [3]: type(p)

Out[3]: _sre.SRE_Pattern

In [4]: p.search('I love FishC.com')

Out[4]: <_sre.SRE_Match at 0x2bd91d0>

In [5]: p.findall('I love FishC.com')

Out[5]: ['I', 'F', 'C']

In [8]: p.<TAB>

p.findall     p.flags       p.groups      p.pattern     p.search      p.sub        

p.finditer    p.groupindex  p.match      p.scanner     p.split       p.subn 

In [8]: help(p.findall)   #(找到RE匹配的所有子串,将它们作为列表返回)

findall(string[, pos[,endpos]]) --> list.

In [10]: print p   #(编译后的正则表达式为对象,可在该对象上应用以上方法,()中直接跟要查找的字符串)

<_sre.SRE_Pattern object at0x268cc10>

In [23]: help(p.finditer)   #(找到RE匹配的所有子串,将它们作为迭代器返回)

finditer(string[, pos[,endpos]]) --> iterator.

In [24]: help(p.match)   #(决定RE是否在刚开始的位置匹配)

match(string[, pos[,endpos]]) --> match object or None.

In [25]: help(p.search)   #(扫描字符串,找到这个RE匹配的位置)

search(string[, pos[,endpos]]) --> match object or None.

注:

p.match()p.search()若没匹配到,它们将返回None,否则返回MatchObject的实例(内存地址),MatchObject所支持的方法:group()start()end()span();在实际程序中,最常见的做法是将MatchObject保存在一个变量里,检查是否为None

In [6]: m=p.match('I love FishC.com!')

In [7]: m.<TAB> 

m.end       m.expand     m.groupdict  m.lastgroup m.pos        m.regs       m.start     

m.endpos    m.group      m.groups     m.lastindex  m.re        m.span       m.string 

In [7]: help(m.group)   #(返回被RE匹配的字符串)

group([group1, ...]) -> str or tuple.

   Return subgroup(s) of the match by indices or names.

   For 0 returns the entire match.

In [8]: help(m.start)   #(返回匹配刚开始的位置)

start([group=0]) -> int.

   Return index of the start of the substring matched by group.

In [9]: help(m.end)   #(返回匹配结束的位置)

end([group=0]) -> int.

   Return index of the end of the substring matched by group.

In [10]: help(m.span)   #(返回一个元组包含匹配开始、结束的位置)

span([group]) -> tuple.

   For MatchObject m, return the 2-tuple (m.start(group), m.end(group)).

In [20]: p=re.compile(r'[a-z]')

In [21]: m=p.search('I love FishC')

In [22]: m.group()

Out[22]: 'l'

 

举例(match()search()方法在程序中常用来判断是否匹配到):

[root@localhost ~]# vim re_match.py

------------------script start-------------

#!/usr/bin/python2.7

#filename:re_match.py

import re

p=re.compile(r'[a-z]')

m=p.match('string123')

if m:

       print 'Match found:',m.group()

else:

       print 'No match'

--------------script end----------------

[root@localhost ~]# python2.7 re_match.py

Match found: s

 

举例:

In [8]: str1='I love FishC.com!'

In [10]: re.search(r'FishC',str1)

Out[10]: <_sre.SRE_Match at0x24fa648>

In [11]: re.findall(r'FishC',str1)  

Out[11]: ['FishC']

In [12]: re.findall(r'.',str1)   #(点号表示换行符以外的任意字符,此处全部匹配到)

Out[12]:

['I',

 ' ',

 'l',

 'o',

 'v',

 'e',

 ' ',

 'F',

 'i',

 's',

 'h',

 'C',

 '.',

 'c',

 'o',

 'm',

 '!']

In [13]: re.findall(r'Fish.',str1)

Out[13]: ['FishC']

In [14]: re.findall(r'Fish\.',str1)   #(加入转义符表示元字符本身,此处未匹配到内容)

Out[14]: []

In [15]: re.findall(r'\d','I loveFishC.com!123')   #\d表示单个任意数字)

Out[15]: ['1', '2', '3']

In [16]: re.findall(r'\d\d\d','123 456FishC')

Out[16]: ['123', '456']

In [18]: re.findall(r'[aeiou]','string')   #[]创建字符类,仅匹配中括号中的一个字符)

Out[18]: ['i']

In [19]: re.findall(r'[A-Z]','String')   #(正则中默认是打开大小写敏感的,可用re.IGNORECASE取消)

Out[19]: ['S']

In [20]: re.findall(r'[a-z]','Hello')

Out[20]: ['e', 'l', 'l', 'o']

In [21]:re.findall(r'[a-z]','HELLO',re.IGNORECASE)

Out[21]: ['H', 'E', 'L', 'L', 'O']

In [22]: re.findall(r'ab{3}c','abbbc')   #{NUM}表示重复的次数)

Out[22]: ['abbbc']

In [23]: re.findall(r'ab{3,10}c','abbbbc')

Out[23]: ['abbbbc']

In [24]: re.findall(r'[0-255]','188')   #(注意此处表示012后面跟两个5

Out[24]: ['1']

In [25]: re.findall(r'[0-255]','1155')

Out[25]: ['1', '1', '5', '5']

In [28]:re.findall(r'(?<![\.\d])(?:\d{1,3}\.){3}\d{1,3}(?![\.\d])','192.168.1.1')   #(匹配IP

Out[28]: ['192.168.1.1']

In [29]:re.findall(r'(?<![\.\d])(?:\d{1,3}\.){3}\d{1,3}(?![\.\d])','192.168.23.125')

Out[29]: ['192.168.23.125']

In [30]:re.findall(r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$','127.0.0.1')

Out[30]: ['127.0.0.1']

In [31]: re.findall(r'Fish(C|D)','www.FishC.com')   #(若使用分组(),默认仅显示匹配到的分组内的内容)

Out[31]: ['C']

In [33]: re.findall(r'^FishC','FishC.com')

Out[33]: ['FishC']

In [34]: re.findall(r'FishC$','I loveFishC')

Out[34]: ['FishC']

In [35]:re.findall(r'(FishC)\1','FishCFishC')  #\后跟序号1,引用序号对应的子组所匹配的字符串,子组的序号从1开始计算)

Out[35]: ['FishC']

In [36]: re.findall(r'\.','FishC.com')   #\.表示点本身)

Out[36]: ['.']

In [37]: re.findall(r'[.]','FishC.com')   #(中括号中的元字符代表其本身,不具备元字符的功能)

Out[37]: ['.']

In [38]: re.findall(r'[\n]','FishC.com\n')   #(匹配回车符)

Out[38]: ['\n']

In [39]: re.findall(r'[^a-z]','FishC.com')   #[]中的^是取反,若取反^只能放在中括号的最前面)

Out[39]: ['F', 'C', '.']

In [40]: re.findall(r'FishC{3}','FishCCCC')   #{NUM}仅对前面的单个字符做重复)

Out[40]: ['FishCCC']

In [41]: re.findall(r'(FishC){2,5}','FishCFishCFishC')

Out[41]: ['FishC']

In [42]:re.findall(r'<.+>','<html><title>I loveFishC</title></html>')   #(正则默认是贪婪模式,在条件符合时尽可能多的匹配,可在.+后加上?进入非贪婪模式)

Out[42]: ['<html><title>I loveFishC</title></html>']

In [43]:re.findall(r'<.+?>','<html><title>I love FishC</title></html>')

Out[43]: ['<html>', '<title>','</title>', '</html>']

In [46]: re.findall(r'\bFishC\b','FishC.comFishC_com FishC')   #(匹配单词边界,下划线也是单词的一部分)

Out[46]: ['FishC', 'FishC']

In [48]: re.findall(r'py\B','python py2py3')   #(匹配非单词边界)

Out[48]: ['py', 'py', 'py']

In [49]: re.findall(r'py\B','py py. py!')

Out[49]: []

In [18]:re.findall(r'^010-?\d{8}','010-12345678')

Out[18]: ['010-12345678']

In [19]: re.findall(r'ab+','abbbbc')    #(贪婪模式)

Out[19]: ['abbbb']

In [20]: re.findall(r'ab+?','abbbbbc')    #(非贪婪模式,最小匹配)

Out[20]: ['ab']

In [23]: re.findall(r'csvt.net','csvt.net')

Out[23]: ['csvt.net']

In [24]:re.findall(r'csvt.net','csvt\nnet')

Out[24]: []

In [25]:re.findall(r'csvt.net','csvt\nnet',re.S)  #(开启了re.DOTALL使得点号可顶替任何字符,包括换行符)

Out[25]: ['csvt\nnet']

举例(标志位re.Mre.MULTILINE):

In [2]: s='''

  ...: hello csvt

  ...: csvt hello

  ...: hello csvt

  ...: csvt hello

  ...: '''

In [3]: re.findall(r'^csvt',s)   #(不开re.M标志位的情况下,是无法匹配多行的)

Out[3]: []

In [4]: re.findall(r'^csvt',s,re.M)

Out[4]: ['csvt', 'csvt']

举例(标志位re.Xre.VERBOSE,用于正则的书写为多行时):

In [5]: tel=r'''

  ...: \d{3,4}

  ...: -?

  ...: \d{8}

  ...: '''

 

In [6]: re.findall(tel,'010-12345678')

Out[6]: []

In [7]: re.findall(tel,'010-12345678',re.X)

Out[7]: ['010-12345678']

举例(分组):

In [8]:re.findall(r'\w{3,}@\w+(\.com|\.cn)','zzz@csvt.com')   #(当有分组时findall会优先返回查找到的分组中的内容)

Out[8]: ['.com']

In [9]: re.findall(r'\w{3,}@\w+(\.com|\.cn)','zzz@csvt.cn')

Out[9]: ['.cn']

In [10]:re.findall(r'\w{3,}@\w+(\.com|\.cn)','zzz@csvt.org')

Out[10]: []

In [11]:re.match(r'\w+@\w+(\.com|\.cn)','jowin@163.com')

Out[11]: <_sre.SRE_Match at0x22e8198>

In [12]: s='''

   ....: aaa srt hello src=csvt

  ....: yes

  ....: hello src=python hello

  ....: '''

In [13]: re.findall(r'hello src=(.+)',s)   #(查找指定内容中包含hello src=……的内容)

Out[13]: ['csvt', 'python hello']

 


str字符串替换:

In [5]: help(str.replace)

S.replace(old, new[,count]) -> string

In [6]: s='hello csvt'

In [7]: s.replace('csvt','python')

Out[7]: 'hello python'

In [9]: s.replace(r'c..t','python')   #(字符串所支持的方法和操作是不支持re的)

Out[9]: 'hello csvt'

 

 

re.sub()re.subn()(正则中的替换):

In [10]: help(re.sub)

sub(pattern, repl,string, count=0, flags=0)

In [11]: help(re.subn)

subn(pattern, repl,string, count=0, flags=0)

In [13]: re.sub(r'c..t','csvt','caat cvvt ccct')

Out[13]: 'csvt csvt csvt'

In [14]: re.subn(r'c..t','csvt','caat cvvt ccct')   #subn不仅打印替换的内容,还打印替换了几次)

Out[14]: ('csvt csvt csvt', 3)

 

 

str.split()

In [16]: help(str.split)

S.split([sep[,maxsplit]]) -> list of strings

In [17]: ip='192.168.23.128'

In [18]: ip.split('.')

Out[18]: ['192', '168', '23', '128']

 

re.split

In [15]: help(re.split)

split(pattern, string,maxsplit=0, flags=0)

In [20]: re.split(r'[\+\-\*]','1+2-3*4')   #(中括号中的元字符将失去本身的意义,但此例中-表示范围,在不加转义符的前提下,要么-不能放在两个元字符中间,要么将-放到最后即可)

Out[20]: ['1', '2', '3', '4']

In [21]: re.split(r'[+*]','1+2*3')

Out[21]: ['1', '2', '3']

In [22]: re.split(r'[+*-]','1+2-3*4')

Out[22]: ['1', '2', '3', '4']



 

MySQLdb模块(软件包名是MySQL-python-1.2.5.tar.gz):

[root@localhost ~]# yum -y installpython-devel mysql-devel zlib-devel openssl-devel gcc

[root@localhost setuptools-19.6.1]#python2.7 setup.py install   (要先安装setuptools-19.6.1.tar.gz这个模块)

[root@localhost MySQL-python-1.2.5]#python2.7 setup.py install

……

Installed/usr/local/python2.7/lib/python2.7/site-packages/MySQL_python-1.2.5-py2.7-linux-x86_64.egg

Processing dependencies forMySQL-python==1.2.5

Finished processing dependencies forMySQL-python==1.2.5

 

注:通过pythonMySQLdb模块与mysql交互,三步骤:

1)创建连接、创建游标,conn=MySQLdb.connect(user='root',password='secret','host'='IP')cur=conn.cursor()

2)选择库、发送执行指令进行增删改查操作,conn.select_db('DB')cur.execute('mysqlcommand')

3)关闭,cur.close()conn.close()

注:增删改查只用传递一次即可,而查询要两次(先用cur.execute('select * from table')传送再用cur.fetchone()查看记录),fetchone()每次查看一条记录并有指针的移动,可用cur.scroll(0,'absolute')将指针移到刚开始处;

注:查询也可合并cur.fetchmany(cur.execute('select * form table')

 

In [1]: import MySQLdb

In [2]: MySQLdb.<TAB>

MySQLdb.BINARY                                  MySQLdb.Timestamp

MySQLdb.Binary                                  MySQLdb.TimestampFromTicks

MySQLdb.Connect                                  MySQLdb.Warning

MySQLdb.Connection                               MySQLdb.apilevel

MySQLdb.DATE                                     MySQLdb.connect

MySQLdb.DATETIME                                MySQLdb.connection

MySQLdb.DBAPISet                                MySQLdb.constants

MySQLdb.DataError                                MySQLdb.debug

MySQLdb.DatabaseError                            MySQLdb.escape

MySQLdb.Date                                    MySQLdb.escape_dict

MySQLdb.DateFromTicks                           MySQLdb.escape_sequence

MySQLdb.Error                                   MySQLdb.escape_string

MySQLdb.FIELD_TYPE                              MySQLdb.get_client_info

MySQLdb.IntegrityError                           MySQLdb.paramstyle

MySQLdb.InterfaceError                           MySQLdb.release

MySQLdb.InternalError                            MySQLdb.result

MySQLdb.MySQLError                              MySQLdb.server_end

MySQLdb.NULL                                     MySQLdb.server_init

MySQLdb.NUMBER                                  MySQLdb.string_literal

MySQLdb.NotSupportedError                       MySQLdb.test_DBAPISet_set_equality

MySQLdb.OperationalError                        MySQLdb.test_DBAPISet_set_equality_membership

MySQLdb.ProgrammingError                        MySQLdb.test_DBAPISet_set_inequality

MySQLdb.ROWID                                   MySQLdb.test_DBAPISet_set_inequality_membership

MySQLdb.STRING                                  MySQLdb.thread_safe

MySQLdb.TIME                                    MySQLdb.threadsafety

MySQLdb.TIMESTAMP                                MySQLdb.times

MySQLdb.Time                                     MySQLdb.version_info

MySQLdb.TimeFromTicks                           

In [2]: help(MySQLdb.connect)

Connect(*args, **kwargs)

   Factory function for connections.Connection.

In [5]: conn.<TAB>

conn.DataError               conn.close                  conn.get_host_info           conn.set_character_set

conn.DatabaseError           conn.commit                  conn.get_proto_info          conn.set_server_option

conn.Error                   conn.converter               conn.get_server_info         conn.set_sql_mode

conn.IntegrityError          conn.cursor                  conn.info                    conn.show_warnings

conn.InterfaceError          conn.cursorclass             conn.insert_id               conn.shutdown

conn.InternalError           conn.default_cursor          conn.kill                    conn.sqlstate

conn.NotSupportedError       conn.dump_debug_info         conn.literal                 conn.stat

conn.OperationalError        conn.encoders                conn.messages                conn.store_result

conn.ProgrammingError        conn.errno                   conn.next_result             conn.string_decoder

conn.Warning                 conn.error                   conn.open                    conn.string_literal

conn.affected_rows           conn.errorhandler            conn.ping                    conn.thread_id

conn.autocommit              conn.escape                  conn.port                    conn.unicode_literal

conn.begin                   conn.escape_string           conn.query                   conn.use_result

conn.change_user             conn.field_count             conn.rollback                conn.warning_count

conn.character_set_name      conn.get_autocommit          conn.select_db              

conn.client_flag             conn.get_character_set_info  conn.server_capabilities 

In [5]: help(conn.cursor)

cursor(self,cursorclass=None) method of MySQLdb.connections.Connection instance

   Create a cursor on which queries may be performed. The

   optional cursorclass parameter is used to create the

   Cursor. By default, self.cursorclass=cursors.Cursor is

   used.

In [6]: help(conn.select_db)

select_db(...)

   Causes the database specified by db to become the default

   (current) database on the connection specified by mysql. In subsequent

   queries, this database is the default for table references that do not

   include an explicit database specifier.

In [8]: cur.<TAB>

cur.DataError          cur.NotSupportedError  cur.connection         cur.fetchmany          cur.scroll

cur.DatabaseError      cur.OperationalError   cur.description        cur.fetchone           cur.setinputsizes

cur.Error              cur.ProgrammingError   cur.description_flags  cur.lastrowid          cur.setoutputsizes

cur.IntegrityError     cur.Warning            cur.errorhandler       cur.messages          

cur.InterfaceError     cur.arraysize          cur.execute            cur.nextset           

cur.InternalError      cur.callproc           cur.executemany        cur.rowcount          

cur.MySQLError         cur.close              cur.fetchall           cur.rownumber   

In [32]: help(cur.execute)

execute(self, query,args=None) method of MySQLdb.cursors.Cursor instance

   Execute a query.

In [33]: help(cur.executemany)

executemany(self, query,args) method of MySQLdb.cursors.Cursor instance

   Execute a multi-row query.

In [34]: help(cur.fetchone)

fetchone(self) method ofMySQLdb.cursors.Cursor instance

   Fetches a single row from the cursor. None indicates that

    no more rows are available.

In [35]: help(cur.fetchmany)

fetchmany(self,size=None) method of MySQLdb.cursors.Cursor instance

   Fetch up to size rows from the cursor. Result set may be smaller

   than size. If size is not defined, cursor.arraysize is used.

In [36]: help(cur.scroll)

scroll(self, value,mode='relative') method of MySQLdb.cursors.Cursor instance

   Scroll the cursor in the result set to a new position according

   to mode.

 

 

In [4]:conn=MySQLdb.connect(user='root',passwd='chai',host='127.0.0.1')

In [7]: cur=conn.cursor()

 

In [8]: conn.select_db('webgame')

In [27]: cur.execute('insert into temp1(accountid,nickname,serverid) value (1,"jowin",14115)')

Out[27]: 1L

In [28]: sqli='insert into temp1(accountid,nickname,serverid) values (%s,%s,%s)'

In [29]: cur.execute(sqli,(2,'snow',14116))

Out[29]: 1L

In [31]:cur.executemany(sqli,[(3,'maisie',14117),(4,'emilia',14118),(5,'lsaac',14119)])

Out[31]: 3L

In [37]: cur.execute('select * from temp1')

Out[37]: 5L

In [38]: cur.fetchone()   #(每查询一条有指针的移动)

Out[38]: (1L, 'jowin', 14115L)

In [39]: cur.fetchone()

Out[39]: (2L, 'snow', 14116L)

In [40]: cur.fetchone()

Out[40]: (3L, 'maisie', 14117L)

In [41]: cur.fetchone()

Out[41]: (4L, 'emilia', 14118L)

In [42]: cur.fetchone()

Out[42]: (5L, 'lsaac', 14119L)

In [43]: cur.fetchone()

In [44]: cur.scroll(0,'absolute')   #(将指针移到开始位置)

In [45]: cur.fetchmany(5)

Out[45]:

((1L, 'jowin', 14115L),

 (2L,'snow', 14116L),

 (3L,'maisie', 14117L),

 (4L,'emilia', 14118L),

 (5L,'lsaac', 14119L))

In [47]: cur.fetchmany(cur.execute('select* from temp1'))   #(查询两步合为一步)

Out[47]:

((1L, 'jowin', 14115L),

 (2L,'snow', 14116L),

 (3L,'maisie', 14117L),

 (4L,'emilia', 14118L),

 (5L,'lsaac', 14119L))

 

In [48]: cur.close()

In [49]: conn.close()



本文转自 chaijowin 51CTO博客,原文链接:http://blog.51cto.com/jowin/1791578,如需转载请自行联系原作者
相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
16天前
|
存储 开发者 Python
Python中的collections模块与UserDict:用户自定义字典详解
【4月更文挑战第2天】在Python中,`collections.UserDict`是用于创建自定义字典行为的基类,它提供了一个可扩展的接口。通过继承`UserDict`,可以轻松添加或修改字典功能,如在`__init__`和`__setitem__`等方法中插入自定义逻辑。使用`UserDict`有助于保持代码可读性和可维护性,而不是直接继承内置的`dict`。例如,可以创建一个`LoggingDict`类,在设置键值对时记录操作。这样,开发者可以根据具体需求定制字典行为,同时保持对字典内部管理的抽象。
|
18天前
|
存储 缓存 算法
Python中collections模块的deque双端队列:深入解析与应用
在Python的`collections`模块中,`deque`(双端队列)是一个线程安全、快速添加和删除元素的双端队列数据类型。它支持从队列的两端添加和弹出元素,提供了比列表更高的效率,特别是在处理大型数据集时。本文将详细解析`deque`的原理、使用方法以及它在各种场景中的应用。
|
2天前
|
索引 Python
python 格式化、set类型和class类基础知识练习(上)
python 格式化、set类型和class类基础知识练习
21 0
|
3天前
|
Python
python学习14-模块与包
python学习14-模块与包
|
3天前
|
Python
python基础知识
【4月更文挑战第15天】python基础知识
17 7
|
5天前
|
SQL 关系型数据库 数据库
Python中SQLite数据库操作详解:利用sqlite3模块
【4月更文挑战第13天】在Python编程中,SQLite数据库是一个轻量级的关系型数据库管理系统,它包含在一个单一的文件内,不需要一个单独的服务器进程或操作系统级别的配置。由于其简单易用和高效性,SQLite经常作为应用程序的本地数据库解决方案。Python的内置sqlite3模块提供了与SQLite数据库交互的接口,使得在Python中操作SQLite数据库变得非常容易。
|
10天前
|
索引 Python
「Python系列」Python operator模块、math模块
Python的`operator`模块提供了一系列内置的操作符函数,这些函数对应于Python语言中的内建操作符。使用`operator`模块可以使代码更加清晰和易读,同时也能提高性能,因为它通常比使用Python内建操作符更快。
27 0
|
14天前
|
数据采集 网络协议 API
python中其他网络相关的模块和库简介
【4月更文挑战第4天】Python网络编程有多个流行模块和库,如requests提供简洁的HTTP客户端API,支持多种HTTP方法和自动处理复杂功能;Scrapy是高效的网络爬虫框架,适用于数据挖掘和自动化测试;aiohttp基于asyncio的异步HTTP库,用于构建高性能Web应用;Twisted是事件驱动的网络引擎,支持多种协议和异步编程;Flask和Django分别是轻量级和全栈Web框架,方便构建不同规模的Web应用。这些工具使网络编程更简单和高效。
|
18天前
|
数据采集 数据挖掘 Python
Python中collections模块的Counter计数器:深入解析与应用
在Python的`collections`模块中,`Counter`是一个强大且实用的工具,它主要用于计数可哈希对象。无论是统计单词出现的频率,还是分析数据集中元素的分布情况,`Counter`都能提供快速且直观的结果。本文将深入解析`Counter`计数器的原理、用法以及它在实际应用中的价值。
|
19天前
|
Python
Python中的math和cmath模块:数学运算的得力助手
Python作为一种功能强大的编程语言,提供了丰富的数学运算功能。其中,math和cmath模块就是Python中用于数学运算的重要工具。math模块提供了基本的数学函数和常量,适用于实数运算;而cmath模块则提供了对复数运算的支持,使得Python在数学计算和工程应用中更加灵活和强大。

热门文章

最新文章