【发布时间】:2011-02-01 12:55:07
【问题描述】:
我正在编写一个小工具来从字符串(通常是推文)中提取一堆值。
字符串可以由单词和数字以及以货币符号(£、$、€ 等)为前缀的金额和多个主题标签(#foo #bar)组成。我在 appEngine 上运行并使用 tweepy 引入推文。
我必须找到值的当前代码如下:
tagex = re.compile(r'#.*')
curex = re.compile(ur'[£].*')
for x in api.user_timeline(since_id = t.lastimport):
tags = re.findall(tagex, x.text)
amount = re.findall(curex, x.text)[0]
logging.info("Text: " + x.text)
logging.info("Tags: " + str(tags))
logging.info("Amount: " + amount)
其中 x.text 是例如“Taxi London £6.50 #projectfoo #clientmeeting”
tagex 发现主题标签很好,但我无法让curex 提取当前获得的数量: 金额:6.50 英镑 #projectfoo #clientmeeting。
我还需要分离货币符号,以便将金额作为浮点数,但稍后应该很简单。
【问题讨论】: