如何拆分列表中的字符串并将每个单词与一组关键字进行比较

2024-04-26 09:59:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一份清单,内容如下:

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'被转换成{},所以在worn中我们有{} 输出为:-

210  
900  
10950
900  
210  
10950
30   
30   

但我期望的输出是30*1ie,'30'


Tags: 字符串innewfor关键字dayslistreader
3条回答
import csv     # imports the csv module

f = open('file.csv', 'rb') # opens the csv file
results = []
try:
    reader = csv.reader(f)  # creates the reader object
    for row in reader:   # iterates the rows of the file in orders
        l = row[0].split(' ')
        if 'day' in l[2]:
            l[1] = int(l[1]) * 1
        elif 'working' in l[2]:
            if len(l) > 3  and 'day' in l[3]:
                l[1] = int(l[1]) * 1
        elif 'week' in l[2]:
            l[1] = int(l[1]) * 7
        elif 'month' in l[2]:
            l[1] = int(l[1]) * 30
        elif 'year' in l[2]:
            l[1] = int(l[1]) * 365
        results.append(l)

finally:
    print results
    f.close()      # closing

你可以先创建一个词典: dictionnary = {"day":1, "month":30 ... }

使用拆分字符串,例如:

splitted_string = ["for", 30, "working", "days"]
interesting_words = splitted_string[2:] # ["working", "days"]

从那里,你可以得到元素“days”并在你的字典中找到相应的元素。找到元素后,我们只需获取值并中断循环。在

^{pr2}$

您最终可以执行您的操作:

result = mult*splitted_string[1] #30

如果你的数据在一个列表中,你可以迭代它。然后拆分每个字符串并在列表末尾搜索关键字('day' in ' '.join(data_split[2:])):

data = ['for 30 days',
    'for 40 working days',
    'for 20 weeks',
    'for 2 months']

for d in data:
    data_split = d.split(' ')
    if 'day' in ' '.join(data_split[2:]):
        print(int(data_split[1]))
    elif 'month' in ' '.join(data_split[2:]):
        print(int(data_split[1]) * 30)

相关问题 更多 >