繁体   English   中英

python 中的正则表达式(用 # 替换数字并删除所有其他字符)

[英]Regex in python (replace digits with # and remove all other characters)

我试图用 # 替换字符串中的每个数字并删除所有其他数字。 我没有得到列表格式的答案。

import re
def replace(str):
    for i in str:
        if 48 <= ord(i) >= 57:
            print(i.replace(i, "#"))
        else:
            print(i.replace(i, " "))
str_1 = "adgf*^%12345"
replace(str_1)

我试图用 # 替换字符串中的每个数字并删除所有其他数字。 我没有得到列表格式的答案。

import re
def replace(str):
    for i in str:
        if 48 <= ord(i) >= 57:
            print(i.replace(i, "#"))
        else:
            print(i.replace(i, " "))
str_1 = "adgf*^%12345"
replace(str_1)

我试图用 # 替换字符串中的每个数字并删除所有其他数字。 我没有得到列表格式的答案。

import re
def replace(str):
    for i in str:
        if 48 <= ord(i) >= 57:
            print(i.replace(i, "#"))
        else:
            print(i.replace(i, " "))
str_1 = "adgf*^%12345"
replace(str_1)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM