我有价格类似:查找货币代码在价格串
$ 1.99
2,99 €
我将如何提取的货币符号?它应该是'空间+符号'或'符号+空间'。这是我目前有的,但它不适用于第二种情况:
s='2,99 €'
>>> re.findall('\s\w',s)
什么是提取货币符号的最佳方法?
我有价格类似:查找货币代码在价格串
$ 1.99
2,99 €
我将如何提取的货币符号?它应该是'空间+符号'或'符号+空间'。这是我目前有的,但它不适用于第二种情况:
s='2,99 €'
>>> re.findall('\s\w',s)
什么是提取货币符号的最佳方法?
我知道这一个不使用re
,但它可能会有所帮助。拆分字符串,只是检查它是否有符号:
s = '2,99 €'
for i in s.split(' '):
if i == '€':
# That's my currency!
如果你知道它将在开始或结束时只是检查那里?
currency_code = None
currency_codes = ("$", "€",)
if len(s) > 0:
if s[0] in currency_codes:
currency_code = s[0]
elif s[-1] in currency_codes:
currency_code = s[-1]
你可以用正则表达式做,当然这似乎是矫枉过正?
这是非常,非常贫民窟,但...的
import string
filter = string.digits + ' ,.'
currency = [ch for ch in s if ch not in filter][0]
可能重复[什么是货币符号的正则表达式?](http://stackoverflow.com/questions/25978771/what-is-regex-对于货币符号) – Andy