簡體   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