[英]Problem with a Python for loop used to modify text file line by line with a condition
I have a Q&D todo file named : DateLoopTest.txt 我有一个名为Q.D的待办事项文件:DateLoopTest.txt
190121 [3] tidy up my office
190122 take my clothes to the laundry
190123 go to the library #(today)
190124 [7] wash my car
and I want to end up with : 我想结束:
190124 [3] tidy up my office
190122 take my clothes to the laundry
190123 go to the library
190124 [7] wash my car
by incrementind the date with the digits provided between brackets, only of course if the date is earlier than today. 通过递增查找日期,并在方括号之间提供数字,当然,前提是日期早于今天。
This is the code I came up with (I use Pythonista on iOs) : 这是我想出的代码(我在iO上使用Pythonista):
#coding: utf-8
import time
import datetime
from datetime import datetime
import re
with open('DateLoopTest.txt') as f:
for line in f:
digits = re.findall(r'\d+', f)
startdateshort = digits[0]
interval = int(digits[1])
startdateepoch = int(datetime.strptime(startdateshort, '%y%m%d').strftime("%s"))+43200
enddateepoch = startdateepoch+interval*86400
enddateshort = time.strftime('%y%m%d', time.localtime(enddateepoch))
now = time.time()
if now > startdateepoch:
newline = re.sub('\d{6}', str(enddateshort), line)
print(newline)
and… nothing happens (except a message (concerning the definition of "digits" : "expected string or byte-like object". 和…什么都没有发生(除了一条消息(关于“数字”的定义:“期望的字符串或类似字节的对象”)。
What went wrong ? 什么地方出了错 ?
Thanks in advance 提前致谢
This is one approach. 这是一种方法。
import re
import datetime
s = """190121 [3] tidy up my office
190122 take my clothes to the laundry
190123 go to the library #(today)
190124 [7] wash my car"""
now = datetime.datetime.now()
for line in s.splitlines(): #Iterating Each line
line = line.strip()
m = re.search(r"\[(\d+)\]", line) #Check if increment date in line
if m:
old_date = line.split()[0] #Get Old date
dateVal = datetime.datetime.strptime(old_date, '%y%m%d') #Convert string to datetime object.
if now > dateVal: #Check Date
print(line.replace(old_date, (dateVal + datetime.timedelta(days=int(m.group(1)))).strftime('%y%m%d'))) #Update
else:
print(line)
else:
print(line)
Output: 输出:
190124 [3] tidy up my office
190122 take my clothes to the laundry
190123 go to the library #(today)
190124 [7] wash my car
You need this: 你需要这个:
#coding: utf-8
import time
import datetime
from datetime import datetime
import re
from tempfile import mkstemp
from shutil import move
from os import fdopen, remove
fh, tmpfilepath = mkstemp()
path = '/Users/earncef/Downloads/Dateloops.txt'
with fdopen(fh, 'w') as tmpfile:
with open(path) as f:
for line in f:
digits = re.findall(r'\d+', line)
startdateshort = digits[0]
interval = 0
if len(digits) > 1:
interval = int(digits[1])
startdateepoch = int(datetime.strptime(startdateshort, '%y%m%d').strftime("%s"))+43200
enddateepoch = startdateepoch+interval*86400
enddateshort = time.strftime('%y%m%d', time.localtime(enddateepoch))
now = time.time()
if now > startdateepoch:
newline = re.sub('\d{6}', str(enddateshort), line)
tmpfile.write(newline)
print(newline)
remove(path)
move(tmpfilepath, path)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.