Ⅰ python如何去除字元串中不想要的字元
問題:
過濾用戶輸入中前後多餘的空白字元
『 ++++abc123--- 『
過濾某windows下編輯文本中的』\r』:
『hello world \r\n』
去掉文本中unicode組合字元,音調
"Zhào Qián Sūn Lǐ Zhōu Wú Zhèng Wáng"
如何解決以上問題?
去掉兩端字元串: strip(), rstrip(),lstrip()
123456789101112131415
#!/usr/bin/python3 s = ' -----abc123++++ ' # 刪除兩邊空字元print(s.strip()) # 刪除左邊空字元print(s.rstrip()) # 刪除右邊空字元print(s.lstrip()) # 刪除兩邊 - + 和空字元print(s.strip().strip('-+'))
刪除單個固定位置字元: 切片 + 拼接
123456
#!/usr/bin/python3 s = 'abc:123'# 字元串拼接方式去除冒號new_s = s[:3] + s[4:]print(new_s)
刪除任意位置字元同時刪除多種不同字元:replace(), re.sub()
1234567891011
#!/usr/bin/python3 # 去除字元串中相同的字元s = '\tabc\t123\tisk'print(s.replace('\t', '')) import re# 去除\r\n\t字元s = '\r\nabc\t123\nxyz'print(re.sub('[\r\n\t]', '', s))
同時刪除多種不同字元:translate() py3中為str.maketrans()做映射
1234567
#!/usr/bin/python3 s = 'abc123xyz'# a _> x, b_> y, c_> z,字元映射加密print(str.maketrans('abcxyz', 'xyzabc'))# translate把其轉換成字元串print(s.translate(str.maketrans('abcxyz', 'xyzabc')))
去掉unicode字元中音調
#!/usr/bin/python3 import sysimport unicodedatas = "Zhào Qián Sūn Lǐ Zhōu Wú Zhèng Wáng"remap = { # ord返回ascii值 ord('\t'): '', ord('\f'): '', ord('\r'): None }# 去除\t, \f, \ra = s.translate(remap)'''通過使用dict.fromkeys() 方法構造一個字典,每個Unicode 和音符作為鍵,對於的值全部為None然後使用unicodedata.normalize() 將原始輸入標准化為分解形式字元sys.maxunicode : 給出最大Unicode代碼點的值的整數,即1114111(十六進制的0x10FFFF)。unicodedata.combining:將分配給字元chr的規范組合類作為整數返回。 如果未定義組合類,則返回0。'''cmb_chrs = dict.fromkeys(c for c in range(sys.maxunicode) if unicodedata.combining(chr(c))) #此部分建議拆分開來理解b = unicodedata.normalize('NFD', a)'''調用translate 函數刪除所有重音符'''print(b.translate(cmb_chrs))
Ⅱ python3 如何去除字元串中不想要的字元
去除不想要的字元有很多種方法:
1、利用python中的replace()方法,把不想要的字元替換成空;
2、利用python的rstrip()方法,lstrip()方法,strip()方法去除收尾不想要的字元。
用法如下:
Python3 replace()方法
Python3 rstrip()方法
Python3 lstrip()方法
Ⅲ python的字元串如何按自己指定的格式進行篩選
按照你的要求編寫的Python程序如下
import re
str = "娜娜_二零零-酷我(ERFD)dgfdg國米_山地車-乘除法(ICEX)ifex憤憤然_概念人-維權(LUVD)cisnq框架內_聚合-阿文看(OMNS)cdwcgr"
regex=r'([u4e00-u9fa5]+_)'
a=re.sub(regex,r' 1',str)
print(a)
Ⅳ 如何使用python去掉指定的字元串
如果字元串是固定為{string}這種格式的可以:
s = '{}'
print(s[1:-2])
如果不是固定的格式:
s = '{}'
print(s.split('{')[1].split('}')[0])
Ⅳ python程序:輸入一個字元串,將其中字母'a'濾掉,生成另一個字元串.怎麼寫
>>> str1 = "abcddcba"
>>> str2 = str1.replace("a","")
>>> str2
'bcddcb'
>>>
Ⅵ python3怎樣過濾字元串中的表情
importre
emoji_pattern=re.compile(
u"(ud83d[ude00-ude4f])|"#emoticons
u"(ud83c[udf00-uffff])|"#symbols&pictographs(1of2)
u"(ud83d[u0000-uddff])|"#symbols&pictographs(2of2)
u"(ud83d[ude80-udeff])|"#transport&mapsymbols
u"(ud83c[udde0-uddff])"#flags(iOS)
"+",flags=re.UNICODE)defremove_emoji(text):
returnemoji_pattern.sub(r'',text)
來自:http://blog.csdn.net/orangleliu/article/details/67632628?utm_source=gold_browser_extension
上面那個有時不好用,
try:
#pythonUCS-4build的處理方式
highpoints=re.compile(u'[U00010000-U0010ffff]')
exceptre.error:
#pythonUCS-2build的處理方式
highpoints=re.compile(u'[uD800-uDBFF][uDC00-uDFFF]')
resovle_value=highpoints.sub(u'??',src_string)
嘗試一下這個。
Ⅶ python中如何使將字元串模式去掉
去掉兩端字元串: strip(), rstrip(),lstrip()
123456789101112131415
#!/usr/bin/python3 s = ' -----abc123++++ ' # 刪除兩邊空字元print(s.strip()) # 刪除左邊空字元print(s.rstrip()) # 刪除右邊空字元print(s.lstrip()) # 刪除兩邊 - + 和空字元print(s.strip().strip('-+'))
刪除單個固定位置字元: 切片 + 拼接
123456
#!/usr/bin/python3 s = 'abc:123'# 字元串拼接方式去除冒號new_s = s[:3] + s[4:]print(new_s)
刪除任意位置字元同時刪除多種不同字元:replace(), re.sub()
1234567891011
#!/usr/bin/python3 # 去除字元串中相同的字元s = '\tabc\t123\tisk'print(s.replace('\t', '')) import re# 去除\r\n\t字元s = '\r\nabc\t123\nxyz'print(re.sub('[\r\n\t]', '', s))
同時刪除多種不同字元:translate() py3中為str.maketrans()做映射
1234567
Ⅷ Python怎麼判斷過濾特殊字元
如果是從Html文本弄來的,那直接download Html的源代碼,然後直接替換其中的<div class="">這里是隨機字元串</div> Python裡面使用xml.minidom就OK!
Ⅸ Python:如何過濾特殊字元和亂碼的字元
如果是從Html文本弄來的,那直接download Html的源代碼,然後直接替換其中的<div class="">這里是隨機字元串</div>
Python裡面使用xml.minidom就OK
Ⅹ python怎麼過濾字元串長度小於4的
len(s) < 4
len函數用於獲取字元串長度,因此上述表達式用於判斷字元串s的長度是否小於4