English 中文(简体)
如何删除 python 中的非数字字符, 并保留一些特殊字符
原标题:How to remove nonalphanumeric character in python but keep some special characters
  • 时间:2012-05-25 09:24:14
  •  标签:
  • python
  • regex

使用以下函数,我找到了如何删除 python 中特殊非字母数字字符的方法 :

p_nonalphanum = re.compile( W+ )

def removeNonAlphaNum(string):
        m = p_nonalphanum.match(string)
        if m:
            string = string[m.end():]
        return string

我想保留一些特殊人物, 尽管我认为这是数字。 我该如何编辑我的 Regex?

<强度>例如:从 " • 1⁄2杯面粉"<强度>到 "1⁄2杯面粉"

最佳回答
>>> def remove_unwanted(s):
...        • ½ cup flour -> ½ cup flour   
...     allowed =  [w½¾]+ 
...     return    .join(re.findall(allowed, s))
... 
>>> print remove_unwanted( • ½ cup flour -> )
½ cup flour
问题回答

您可以使用一个负掉的字符类, 添加您想要保留的所有字符

你可以做这样的事:

p_nonalphanum = re.compile( [^w½¾]+ )
print (p_nonalphanum.sub(  , test))

使用 < code>isalnum 内建函数!

>>> s = "• ½ cup flour -> ½ cup flour"
>>> def only_alphanum(s):
...     s = unicode(s, "utf-8")
...     return    .join(c for c in s.split() if c.isalnum())
... 
>>> print only_alphanum(s)
½ cup flour ½ cup flour

这将让您捕捉到任何分数, 而不是仅仅列出您在正数中组装的分数( 它会很快长) 。

>>> s = "• ¼ cup oats -*(*&!!"
>>> print only_alphanum(s)
¼ cup oats




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签