溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

當Python字符串遇上MySQL

發(fā)布時間:2020-08-16 21:08:34 來源:ITPUB博客 閱讀:234 作者:DicksonJYL560101 欄目:MySQL數(shù)據(jù)庫

    學習的時候我喜歡對比,MySQL和Oracle比,Python和MySQL比,總能有一些收獲,也有了新的理解。

    今天整理這部分內(nèi)容的時候,我發(fā)現(xiàn)Python和MySQL還是有很多相似之處。學習一門語言,一個數(shù)據(jù)庫,字符串的處理都是一個相對重要的部分,所以我決定對比一下兩者的差別。

    下面的演示會一邊Python,一邊MySQL,所以按照這個思路來看就不會感覺突兀了。

  轉義字符

    >>> print '\\'
\

mysql> select '\\';
+---+
| \ |
+---+
| \ |
+---+

>>> print '\"'
"
mysql> select '\"';
+---+
| " |
+---+
| " |
+---+

>>> print '\''
'
mysql> select '\'';
+---+
| ' |
+---+
| ' |
+---+

字符串拼接
>>> x = 'hello'
>>> y = 'tester'
>>> z = x + y
>>> print z
hellotester

set @x='hello';
set @y='tester';
mysql> select @x;
+-------+
| @x    |
+-------+
| hello |
mysql> select @y;
+--------+
| @y     |
+--------+
| tester |
+--------+
mysql> select concat(@x,@y);
+---------------+
| concat(@x,@y) |
+---------------+
| hellotester   |
+---------------+

字符串復制
>>> print '#'*20
####################
mysql> select repeat('#',20);
+----------------------+
| repeat('#',20)       |
+----------------------+
| #################### |
+----------------------+

>>> print ' '*20 + 'end'
                    end
mysql> select space(20);
+----------------------+
| space(20)            |
+----------------------+
|                      |
+----------------------+
字符串截取
>>> name = 'yangjianrong'
>>> name[0]
'y'

>>> name[-1]
'g'
>>> name[1]
'a'
>>> name[1:4]
'ang'

>>> name[:]
'yangjianrong'
>>>
>>> name[1:4:2]
'ag'

mysql> set @name:='yangjianrong';
mysql> select left(@name,1);         
+---------------+
| left(@name,1) |
+---------------+
| y             |
+---------------+
mysql> select right(@name,1);
+----------------+
| right(@name,1) |
+----------------+
| g              |
+----------------+
mysql> select substring(@name,2,3);
+----------------------+
| substring(@name,2,3) |
+----------------------+
| ang                  |
+----------------------+
mysql> select substring(@name,1);
+--------------------+
| substring(@name,1) |
+--------------------+
| yangjianrong       |
+--------------------+
或者使用mid
mysql> select mid(@name,2,3);         
+----------------+
| mid(@name,2,3) |
+----------------+
| ang            |
+----------------+
mysql>  select mid(@name,1);         
+--------------+
| mid(@name,1) |
+--------------+
| yangjianrong |
+--------------+

>>> name
'yangjianrong'

>>> print '%s' %name
yangjianrong

字符串格式化,匹配
>>> '{name},{alias}'.format(name='yangjianrong',alias='jeanron100')    
'yangjianrong,jeanron100'
>>>

mysql>  select concat(insert(@name,1,4,'yangjianrong'),insert(@alias,1,5,'jeanron100')) comm;
+------------------------+
| comm                   |
+------------------------+
| yangjianrongjeanron100 |
+------------------------+
字符串長度>>> ba
'this is a test bar'
>>> len(ba)
18
mysql> select length(@ba);     字符串空格處理

>>> s = ' abc '
>>> s.lstrip()
'abc '
>>> s.rstrip()
' abc'
>>> s.strip()
'abc'
>>>
mysql> set @s=' abc ';
Query OK, 0 rows affected (0.00 sec)
mysql> select ltrim(@s);
+-----------+
| ltrim(@s) |
+-----------+
| abc       |
+-----------+
1 row in set (0.00 sec)

mysql> select rtrim(@s);
+-----------+
| rtrim(@s) |
+-----------+
|  abc      |
+-----------+
1 row in set (0.00 sec)

mysql> select trim(@s);
+----------+
| trim(@s) |
+----------+
| abc      |
+----------+
1 row in set (0.00 sec)

字符串匹配

>>> l = ['a','b','c']
>>> ''.join(l)
'abc'
>>> '*'.join(l)
'a*b*c'

mysql> select concat_ws(',','a','b','c','d','e') comm;
+-----------+
| comm      |
+-----------+
| a,b,c,d,e |
+-----------+
>>> s = 'a b c d e '
>>> s.split(' ')
['a', 'b', 'c', 'd', 'e', '']
mysql> set @s='a b c d e ';
Query OK, 0 rows affected (0.00 sec)

mysql> select replace(@s,' ',',');
+---------------------+
| replace(@s,' ',',') |
+---------------------+
| a,b,c,d,e,          |
+---------------------+

字符串復制
>>> s = 'aabbcc'
>>> s.replace('aa','tt')
'ttbbcc'

mysql> set @s='aabbcc';
Query OK, 0 rows affected (0.00 sec)

mysql> select replace(@s,'aa','tt');
+-----------------------+
| replace(@s,'aa','tt') |
+-----------------------+
| ttbbcc                |
+-----------------------+

 字符串編碼
>>> s.encode('utf8')
'aabbcc'

mysql> select  convert(@s using utf8);
+------------------------+
| convert(@s using utf8) |
+------------------------+
| aabbcc                 |
+------------------------+     判斷字符串開始匹配的字符
>>> s.startswith('aa')
True

mysql> SELECT LOCATE('aa',@s,1);  
+-------------------+
| LOCATE('aa',@s,1) |
+-------------------+
|                 1 |
+-------------------+

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI