在斜杠

时间:2016-05-24 21:07:02

标签: python date csv lambda

如何使用MM/DD/YYYY格式过滤第7列中的月份?

我目前有

monthlist = ['1','2','3','4','5','6','7','8','9','10','11','12']

filtered = filter(lambda p: monthlist == p[7][:1],reader)

它适用于单个数字月份,直到两位数月份(10,11,12)。

是否可以在/之前使用lambda过滤所有内容?

编辑:

读者样本:

                                                          Date                                                                                                 
Apple Pear Orange Cracker Honey Cheese Grape Bread Tomato 5/11/13 Lettuce Carrot
Apple Pear Orange Cracker Honey Cheese Grape Bread Tomato 4/12/13 Lettuce Carrot
Apple Pear Orange Cracker Honey Cheese Grape Bread Tomato 10/11/15 Lettuce Carrot

1 个答案:

答案 0 :(得分:1)

row = 'Apple Pear Orange Cracker Honey Cheese Grape Bread Tomato 5/11/13'.split()
assert row[6] == 'Grape'  # This is the 7th column in your sample
assert row[7] == 'Bread'  # This is the column you were getting

date = row[9]
assert date == '5/11/13'

parts = date.split('/')
assert parts == ['5', '11', '13']
MM = int(parts[0])
assert MM == 5

# Doesn't matter if there's more digits
assert int('27/11/13'.split('/')[0]) == 27