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

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
云数据库 RDS MySQL,高可用系列 2核4GB
简介:

正则表达式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,如需转载请自行联系原作者
相关实践学习
如何快速连接云数据库RDS MySQL
本场景介绍如何通过阿里云数据管理服务DMS快速连接云数据库RDS MySQL,然后进行数据表的CRUD操作。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
3月前
|
Python
在Python中,可以使用内置的`re`模块来处理正则表达式
在Python中,可以使用内置的`re`模块来处理正则表达式
90 5
|
3月前
|
数据采集 Web App开发 iOS开发
如何使用 Python 语言的正则表达式进行网页数据的爬取?
使用 Python 进行网页数据爬取的步骤包括:1. 安装必要库(requests、re、bs4);2. 发送 HTTP 请求获取网页内容;3. 使用正则表达式提取数据;4. 数据清洗和处理;5. 循环遍历多个页面。通过这些步骤,可以高效地从网页中提取所需信息。
|
4月前
|
Python
Python 中常用的内置模块之`re`模块
【10月更文挑战第11天】 `re` 模块是 Python 内置的正则表达式处理工具,支持模式匹配、搜索、替换等功能。通过 `search`、`match`、`findall` 和 `sub` 等函数,结合正则表达式的元字符、分组、贪婪模式等特性,可高效完成文本处理任务。示例代码展示了基本用法,帮助快速上手。
63 1
|
4月前
|
Python
【收藏备用】Python正则表达式的7个实用技巧
【收藏备用】Python正则表达式的7个实用技巧
41 1
|
4月前
|
JavaScript 前端开发 Scala
Python学习十:正则表达式
这篇文章是关于Python中正则表达式的使用,包括re模块的函数、特殊字符、匹配模式以及贪婪与非贪婪模式的详细介绍。
39 0
|
4月前
|
数据安全/隐私保护 Python
Python实用正则表达式归纳
Python实用正则表达式归纳
31 3
|
23天前
|
存储 缓存 Java
Python高性能编程:五种核心优化技术的原理与Python代码
Python在高性能应用场景中常因执行速度不及C、C++等编译型语言而受质疑,但通过合理利用标准库的优化特性,如`__slots__`机制、列表推导式、`@lru_cache`装饰器和生成器等,可以显著提升代码效率。本文详细介绍了这些实用的性能优化技术,帮助开发者在不牺牲代码质量的前提下提高程序性能。实验数据表明,这些优化方法能在内存使用和计算效率方面带来显著改进,适用于大规模数据处理、递归计算等场景。
58 5
Python高性能编程:五种核心优化技术的原理与Python代码
|
2月前
|
Python
[oeasy]python055_python编程_容易出现的问题_函数名的重新赋值_print_int
本文介绍了Python编程中容易出现的问题,特别是函数名、类名和模块名的重新赋值。通过具体示例展示了将内建函数(如`print`、`int`、`max`)或模块名(如`os`)重新赋值为其他类型后,会导致原有功能失效。例如,将`print`赋值为整数后,无法再用其输出内容;将`int`赋值为整数后,无法再进行类型转换。重新赋值后,这些名称失去了原有的功能,可能导致程序错误。总结指出,已有的函数名、类名和模块名不适合覆盖赋新值,否则会失去原有功能。如果需要使用类似的变量名,建议采用其他命名方式以避免冲突。
52 14
|
2月前
|
分布式计算 大数据 数据处理
技术评测:MaxCompute MaxFrame——阿里云自研分布式计算框架的Python编程接口
随着大数据和人工智能技术的发展,数据处理的需求日益增长。阿里云推出的MaxCompute MaxFrame(简称“MaxFrame”)是一个专为Python开发者设计的分布式计算框架,它不仅支持Python编程接口,还能直接利用MaxCompute的云原生大数据计算资源和服务。本文将通过一系列最佳实践测评,探讨MaxFrame在分布式Pandas处理以及大语言模型数据处理场景中的表现,并分析其在实际工作中的应用潜力。
116 2

热门文章

最新文章

推荐镜像

更多