循环并连接日志文件

时间:2016-06-20 13:42:19

标签: python python-2.7 loops datetime

所以我有一个小的python脚本会询问用户用户名,然后它会从overrustlelogs.net中删除他们的日志以获取某个twitch.tv频道

目前我只有2个列表,一个是一年中的一个,因为日志URL的格式为

  

http://overrustlelogs.net/Channel%20chatlog/MonthName%20Year/user.txt

months = ['September','October','November','December','January','February','March','April','May','June','July','August','September','October','November','December','January','February','March','April','May','June','July','August']
years = ['2014','2014','2014','2014','2015','2015','2015','2015','2015','2015','2015','2015','2015','2015','2015','2015','2016','2016','2016','2016','2016','2016','2016','2016']

正如你可以看到我使用for循环并获取每个循环的索引的方式非常愚蠢,因为我必须每月手动编辑列表。

有什么更好的方法可以做到这一点?我希望程序将日志从2014年9月下载到当前的月份和年份

2 个答案:

答案 0 :(得分:0)

这是一个简单的脚本,可以帮助您。

  1. 使用datetime.timedelta()创建一个包含特定日期之间天数的列表。

  2. 然后它仅使用月份名称和年份(%B-%Y

  3. 进行格式化
  4. 最后使用if not in条件删除重复项,同时循环计算t0t1之间的计算天数。

  5. <强>代码:

    t0 = '21-06-2014'
    t1 = '07-01-2016'
    
    #formatting in the proper way to use timedelta()
    start = datetime.datetime.strptime(t0, "%d-%m-%Y")
    end = datetime.datetime.strptime(t1, "%d-%m-%Y")
    
    #generating dates using list comprehension and timedelta()
    date_generated = [start + datetime.timedelta(days=x) for x in range(0, (end-start).days)]
    
    #removing duplicates using loop and 'if not in'
    dates = []
    
    for date in date_generated:
        s = str(date.strftime("%B-%Y"))
        if s not in dates:
            dates.append(s)
    
    #printing dates    
    for date in dates:
        print date
    

    <强>输出:

    June-2014
    July-2014
    August-2014
    September-2014
    October-2014
    November-2014
    December-2014
    January-2015
    February-2015
    March-2015
    April-2015
    May-2015
    June-2015
    July-2015
    August-2015
    September-2015
    October-2015
    November-2015
    December-2015
    January-2016
    

    生成dates列表后,我们现在可以使用:

    创建网址
    for date in dates:
        MonthName = date.split('-')[0]
        Year = date.split('-')[1]
        url = 'http://overrustlelogs.net/Channel%20chatlog/' + MonthName + '%20' + Year + '/user.txt'
        print url
    

    <强>输出:

    http://overrustlelogs.net/Channel%20chatlog/June%202014/user.txt
    http://overrustlelogs.net/Channel%20chatlog/July%202014/user.txt
    http://overrustlelogs.net/Channel%20chatlog/August%202014/user.txt
    http://overrustlelogs.net/Channel%20chatlog/September%202014/user.txt
    http://overrustlelogs.net/Channel%20chatlog/October%202014/user.txt
    http://overrustlelogs.net/Channel%20chatlog/November%202014/user.txt
    http://overrustlelogs.net/Channel%20chatlog/December%202014/user.txt
    http://overrustlelogs.net/Channel%20chatlog/January%202015/user.txt
    http://overrustlelogs.net/Channel%20chatlog/February%202015/user.txt
    http://overrustlelogs.net/Channel%20chatlog/March%202015/user.txt
    http://overrustlelogs.net/Channel%20chatlog/April%202015/user.txt
    http://overrustlelogs.net/Channel%20chatlog/May%202015/user.txt
    http://overrustlelogs.net/Channel%20chatlog/June%202015/user.txt
    http://overrustlelogs.net/Channel%20chatlog/July%202015/user.txt
    http://overrustlelogs.net/Channel%20chatlog/August%202015/user.txt
    http://overrustlelogs.net/Channel%20chatlog/September%202015/user.txt
    http://overrustlelogs.net/Channel%20chatlog/October%202015/user.txt
    http://overrustlelogs.net/Channel%20chatlog/November%202015/user.txt
    http://overrustlelogs.net/Channel%20chatlog/December%202015/user.txt
    http://overrustlelogs.net/Channel%20chatlog/January%202016/user.txt
    

答案 1 :(得分:0)

datetime模块外,您还可以查看calendar模块。但是,它没有内置迭代数月。

import calendar
from collections import namedtuple

MonthWithYear = namedtuple('MonthWithYear', ['month', 'year'])

def months_between(since, until):
    def between_including(array, el1, el2):
        since = array.index(el1)
        until = array.index(el2)
        yield from array[since: until + 1]# *including* last element too
    months = calendar.month_name
    if since.year == until.year:
        for month in between_including(list(months), since.month, until.month):
            yield MonthWithYear(month, since.year)
    else:
        yield from months_between(since, MonthWithYear(months[-1], since.year))
        for year in range(since.year + 1, until.year):
            yield from months_between(MonthWithYear(months[1], year), MonthWithYear(months[-1], year))
        yield from months_between(MonthWithYear(months[1], until.year), until)

since = MonthWithYear('January', 2016)
until = MonthWithYear('August', 2019)


for month in months_between(since, until):
    print(month.month, month.year)