Created
June 17, 2020 12:54
-
-
Save jenningsb2/2d94bc286b5fafe878c9e493a0286298 to your computer and use it in GitHub Desktop.
Looks through every file and finds strings that match the date format [[April 20th, 2020]] and converts them to Obsidian sort friendly format [[2020-04-20]].
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
import os | |
from dateutil.parser import parse | |
path = '' #insert file path to your vault here | |
for root, dirs, files in os.walk(path): | |
for f in files: | |
fullpath = (os.path.join(root, f)) | |
with open(fullpath, 'r') as f: #opens each .md file | |
contents = f.read() #reads the contexts | |
#substitutes dates with the format [[April 20th, 2020]] for [[2020-04-20]] | |
new_contents = re.sub(r'(?<=\[)[\w]+\s\d{1,2}\w{1,2},\s\d{4}(?=\])', | |
lambda x: str(parse(x.group(0), ignoretz=True)).split(" ")[0], contents, flags=re.M) | |
with open(fullpath, 'w') as f: | |
f.write(new_contents) #writes the files with the new substitutions |
@dbla-dprout I had to go down the same path. adding 'encoding='utf-8' did help me get further, but I was still getting the error. After adding an error exception, it was able to complete. I verified that (most) dates were converted after this fix:
with open(fullpath, 'r', encoding="utf-8", errors='ignore') as f:
There were three (of hundreds) of dates that weren't fixed, so I'll assume that they were the ones related to the encode error.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Getting an error when attempting to run on Windows in VS Code (I'm a noob).
I've attempted to include explicit encoding on open:
with open(fullpath, 'r', encoding='utf-8') as f: #opens each .md file
But still getting this error....any ideas?