English 中文(简体)
我怎样才能识别数字 与Python的字符串中千人的空间分隔符?
原标题:How can I identify numbers with space separator for thousands in a string with Python?
  • 时间:2012-05-28 14:16:04
  •  标签:
  • python
  • regex

我与使用空格作为千位分隔符的文本合作, 例如 400 或 40 000 或 40 000 000 或 40 000 000 或 4 000 000 000 。 我需要识别字符串中的数字。 一旦识别, 有很多选项可以重新格式化数字 。 在正则上我是一个菜鸟。 这不起作用 :

import re
line =  40) He had 120 hours to increase from 40 000 units to 20 000 000. 
regex = re.compile("(d+ *d+)")
re.findall(regex, line)
[ 40 ,  120 ,  40 000 ,  20 000 ,  000 ]
最佳回答

将开展以下工作:

regex = re.compile(r"(d+(?:s+d+)*)")

这使用一个非捕获组 (? :) , 匹配一个或多个空格( > =%/code > ) 的 < code > ( \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

值得指出的是,对 Python 常规表达式使用原始字符串( r" "r )通常是一个好主意。

最后,我可能要像这样收紧正谱

regex = re.compile(r"(d+(?:s+d{3})*)")

这就要求除第一个数字外,每一组数字的长度为3位数。

问题回答

暂无回答




相关问题
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 ]="...