伊莉討論區
標題: python刪除標點符號 [打印本頁]
作者: yz2243 時間: 2012-10-16 06:48 PM 標題: python刪除標點符號
有人會python的嗎? 把文章裡所有標點符號消除!
求高手幫忙
"If we want to move forward, see Applehealthy and prospering again, we have to let go a few things here. We have tolet go of this notion that for Apple to win, Microsoft has to lose. We have toembrace the notion that for Apple to win, Apple has to do a really good job.And if others are going to help us, that's great … And if we screw up and don'tdo a good job, it's not somebody else's fault -- it's our fault … So the era ofsetting this up as a competition between Apple and Microsoft is over as far asI'm concerned. This is about getting Apple healthy, and this is about Applebeing able to make incredibly great contributions to the industry, to gethealthy and prosper again."
作者: asdf121472 時間: 2013-10-12 01:40 PM
本帖最後由 asdf121472 於 2013-10-12 03:30 PM 編輯
a="If we want to move forward, see Applehealthy and prospering again, we have to let go a few things here. We have tolet go of this notion that for Apple to win, Microsoft has to lose. We have toembrace the notion that for Apple to win, Apple has to do a really good job.And if others are going to help us, that's great … And if we screw up and don'tdo a good job, it's not somebody else's fault -- it's our fault … So the era ofsetting this up as a competition between Apple and Microsoft is over as far asI'm concerned. This is about getting Apple healthy, and this is about Applebeing able to make incredibly great contributions to the industry, to gethealthy and prosper again."
#用ascii碼篩選
for item in a:
if 97<=ord(item)<=122 or 65<=ord(item)<=90 or ord(item)==32 or ord(item)==39:
print(item,end='')
作者: liang628 時間: 2014-9-7 07:47 AM
python 代碼 # -*- coding: gb18030 -*-
import string
import re
identify = string.maketrans('', '')
delEStr = string.punctuation + ' ' + string.digits #ASCII 標點符號,空格和數字
delCStr = '《》()&%¥#@!{}【】'
s = '中華人民共和國(北京)'
s = s.translate(identify, delEStr) #去掉ASCII 標點符號和空格
if re.findall('[\x80-\xff].', s): #s爲中文
s = s.translate(identify, delCStr)
print s
else: #s爲英文
print s
當然,首先是要import string和re(這裏需要正則表達式功能)。
delEStr是一個包含英文標點符號和數字的字符串,就如 '(){}<>1234 ' 這樣。delCStr是一個包含中文標點符號的字符串。s是一個測試字符串。
歡迎光臨 伊莉討論區 (http://a401.file-static.com/) |
Powered by Discuz! |