python相关汇总(增加中)

Python3中字符串中的数字提取方法

totalCount ='100abc'

totalCount = re.sub("\D","", totalCount)

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

在字符串 string 中找到匹配正则表达式 pattern 的所有子串,用另一个字符串 repl 进行替换。如果没有找到匹配 pattern 的串,则返回未被修改的 string。Repl 既可以是字符串也可以是一个函数。由此可分析上面使用的语句的含义:在'100abc'这个字符串中找到非数字的字符(正则表达式中'\D'表示非数字),并用""替换,然后返回的就是只剩下数字的字符串。

含有有小数点正则位可以是[^0-9|.]

pyodbc插入access数据库时不能含 '

所有大写字母,小写字母,数字组成的字符串

string.ascii_lowercase


print[chr(i)foriinrange(65,91)]#所有大写字母

print[chr(i)foriinrange(97,123)]#所有小写字母

print[chr(i)foriinrange(48,58)]#所有数字


import string#导入string这个模块

string.digits#输出包含数字0~9的字符串

string.letters#包含所有字母(大写或小写)的字符串

string.lowercase#包含所有小写字母的字符串

string.uppercase#包含所有大写字母的字符串

string.punctuation#包含所有标点的字符串

string.ascii_letters#与string.letters一样


新加评论 评论标题:

文章评论

    python相关测试

        2018-03-29    
    修改 删除
    Python 3.6.2 (v3.6.2:5fd33b5, Jul  8 2017, 04:57:36) [MSC v.1900 64 bit (AMD64)] on win32
    Type "copyright", "credits" or "license()" for more information.
    >>> import re
    >>> re.findall('(d)\1+','33abcd112')
    []
    >>> len('SDIBT')
    5
    >>> sorted(random.sample(range(5),5))
    Traceback (most recent call last):
      File "<pyshell#3>", line 1, in <module>
        sorted(random.sample(range(5),5))
    NameError: name 'random' is not defined
    >>> import random
    >>> sorted(random.sample(range(5),5))
    [0, 1, 2, 3, 4]
    >>> isinstance('4',(int,float,complex))
    False
    >>> min(['11','2','3'])
    '11'
    >>> g=lambda x,y=3,z=5:x+y+z
    >>> g(2)
    10
    >>> [1,2]+[3]
    [1, 2, 3]
    >>> list(filter(None,[0,1,2,3,0,0]))
    [1, 2, 3]
    >>> int('123',8)
    83
    >>> x=[3,5,7]
    >>> x[:3]=[2]
    >>> x
    [2]
    >>> [index for index,value in enumerate([3,5,7,3,7]) if value==max([3,5,7,3,7])]
    [2, 4]
    >>> (1)+(2)
    3
    >>> 3<<2
    12
    >>> x={'a':'b",'c':'d'}
       
    SyntaxError: invalid syntax
    >>> x={'a':'b','c':'d'}
    >>> 'a' in x
    True
    >>> for i in range(3):print(i,end=',')
    ;
    SyntaxError: invalid syntax
    >>> for i in range(3):print(i,end=',')
    ;
    SyntaxError: invalid syntax
    >>> for i in range(3):print(i,end=',');
    
    0,1,2,
    >>> type(3+4j) in (int,float,complex)
    True
    >>> dir
    <built-in function dir>
    >>> x=(3,)
    >>> x*3
    (3, 3, 3)
    >>> '%c'%65==str(65)
    False
    >>> sum(range(1,10))
    45
    >>> x=[1,2,3,2,3]
    >>> x.remove(2)
    >>> x
    [1, 3, 2, 3]
    >>> x={1:1}
    >>> x[2]=2
    >>> len(x)
    2
    >>> x=3
    >>> x+=6
    >>> x
    9
    >>> x=[1,2]
    >>> x.append([3])
    >>> x
    [1, 2, [3]]
    >>> x=[1,2,3,2,3]
    >>> x.pop()
    3
    x
    >>> 
    >>> x
    [1, 2, 3, 2]
    >>> [1,2,3].count(4)
    0
    >>> int('123')
    123
    >>> 
    ======================== RESTART: F:\myPython\test.py ========================
    Traceback (most recent call last):
      File "F:\myPython\test.py", line 10, in <module>
        sort(i1,i2,i3,i4)
    NameError: name 'sort' is not defined
    >>> 
    ======================== RESTART: F:\myPython\test.py ========================
    Traceback (most recent call last):
      File "F:\myPython\test.py", line 10, in <module>
        sorted(i1,i2,i3,i4)
    TypeError: sorted() takes at most 3 arguments (4 given)
    >>> 
    ======================== RESTART: F:\myPython\test.py ========================
    >>> 
    ======================== RESTART: F:\myPython\test.py ========================
    >>> a
    [5, 5, 5]
    >>> 
    ======================== RESTART: F:\myPython\test.py ========================
    >>> a
    [2, 3, 4]
    >>> 
    ======================== RESTART: F:\myPython\test.py ========================
    >>> a
    [2, 3, 4, 5]
    >>> a[0]
    2
    >>> 
评论列表
python相关测试