![](/img/trans.png)
[英]python replace all special characters and spaces with single '-'
[英]How to replace all those Special Characters with white spaces in python?
如何用python中的空格替换所有这些特殊字符?
我有一个公司名单。 。 。
例如: - [myfiles.txt]
我的公司.C
老酒列兵
大师思想公司
“apex-labs ltd”
“印度新公司”
Indo-American pvt / ltd
这里,按照上面的例子。 。 。 我需要文件中的所有特殊字符[ - ,“,/,。] myfiles.txt
必须替换为单个空格并保存到另一个文本文件myfiles1.txt
。
有人可以帮帮我吗?
假设您要更改所有非字母数字的内容,可以在命令行中执行此操作:
cat foo.txt | sed "s/[^A-Za-z0-99]/ /g" > bar.txt
或者在带有re
模块的Python中:
import re
original_string = open('foo.txt').read()
new_string = re.sub('[^a-zA-Z0-9\n\.]', ' ', original_string)
open('bar.txt', 'w').write(new_string)
import string
specials = '-"/.' #etc
trans = string.maketrans(specials, ' '*len(specials))
#for line in file
cleanline = line.translate(trans)
例如
>>> line = "Indo-American pvt/ltd"
>>> line.translate(trans)
'Indo American pvt ltd'
import re
strs = "how much for the maple syrup? $20.99? That's ricidulous!!!"
strs = re.sub(r'[?|$|.|!]',r'',strs) #for remove particular special char
strs = re.sub(r'[^a-zA-Z0-9 ]',r'',strs) #for remove all characters
strs=''.join(c if c not in map(str,range(0,10)) else '' for c in strs) #for remove numbers
strs = re.sub(' ',' ',strs) #for remove extra spaces
print(strs)
Ans: how much for the maple syrup Thats ricidulous
虽然maketrans是最好的方法,但我从不重写语法。 由于速度很少是一个问题,我知道正则表达式,我倾向于这样做:
>>> line = "-[myfiles.txt] MY company.INC"
>>> import re
>>> re.sub(r'[^a-zA-Z0-9]', ' ',line)
' myfiles txt MY company INC'
这具有额外的好处,即声明您接受的角色而不是您拒绝的角色,在这种情况下感觉更容易。
如果你使用非ASCII字符,你必须回去删除你拒绝的字符。 如果只有标点符号,您可以执行以下操作:
>>> import string
>>> chars = re.escape(string.punctuation)
>>> re.sub(r'['+chars+']', ' ',line)
' myfiles txt MY company INC'
但你会注意到的
起初我想提供一个string.maketrans / translate示例,但也许你正在使用一些utf-8编码的字符串,并且ord()排序的translate-table会吹在你的脸上,所以我想到了另一个解决方案:
conversion = '-"/.'
text = f.read()
newtext = ''
for c in text:
newtext += ' ' if c in conversion else c
这不是最快的方式,但易于掌握和修改。
因此,如果您的文本是非ascii,您可以解码conversion
,将文本字符串解码为unicode,然后以您想要的任何编码重新编码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.