我有一份清单,内容如下:
for 30 days
for 40 working days
for 20 weeks
for 2 months
我想把每个句子分开,并与一组关键字进行比较:
^{pr2}$如果关键字'days'
出现在字符串中,那么我想用'1'
乘以该字符串中的数字。如果关键字'month'
存在,则将该字符串中的数字乘以'30'
,依此类推。。。我是python新手,所以请!在
我的代码
with open("test_term.csv", "rb") as file1:
reader = csv.reader(file1)
extractedlist = list(reader)
#print extractedlist
def split_line(text):
# split the text
words = text[0].split(' ')
# for each word in the line:
new_list = []
for word in words:
#print word
#print w2n.word_to_num(word)
conversion = w2n.word_to_num(word)
if isinstance(conversion, (int,long)):
#print conversion
new_list.append(conversion)
else:
new_list.append(word)
return new_list
for extraRow in extractedlist:
worn = split_line(extraRow)
keywords = {"day":1,"days":1,"year":365,"years":365,"week":7,"weeks":7,"month":30,"months":30}
#for s in worn:
# splitted_string = s.split(' ')
interesting_words = worn[2:]
mult = 1
for k,v in keywords.iteritems():
for word in interesting_words :
mult = v
break
result = mult*worn[1]
print result
现在我只有一个输入字符串for thirty working days
,这里的'thirty'
被转换成{
210
900
10950
900
210
10950
30
30
但我期望的输出是30*1ie,'30'
你可以先创建一个词典:
dictionnary = {"day":1, "month":30 ... }
使用拆分字符串,例如:
从那里,你可以得到元素“days”并在你的字典中找到相应的元素。找到元素后,我们只需获取值并中断循环。在
^{pr2}$您最终可以执行您的操作:
如果你的数据在一个列表中,你可以迭代它。然后拆分每个字符串并在列表末尾搜索关键字(
'day' in ' '.join(data_split[2:])
):相关问题 更多 >
编程相关推荐