Using regex under the if , you can replace the day:
regx = re.compile(ur'(\w+\s\d{1,2},\s\d{4})\s\w{6,9}') line = re.sub(regx, "\\1", line)
Example:
https://regex101.com/r/pJ0nZ8/1
linecache method:
Using the linecache module, you can specifically capture line 5 and write it to a file; if the date includes a weekday, it will be truncated. It is possible to do much more with this functionality, although I will leave more detailed information to you.
import linecache w = 'Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday' l = linecache.getline("Aberdeen2005.txt",5) m = [d in l for d in w] c = '2005','2016' # years (optional) if any(y in l for y in c): # check for years (optional) if any(x in l for x in w): r = [i for i,v in enumerate(m,0) if v] l = l.replace(' '+w[r[0]],'') with open("dates.txt", "a") as article_dates: article_dates.write(l) linecache.clearcache()
l'l'l source share