Skip to content

Instantly share code, notes, and snippets.

@jenningsb2
Created June 17, 2020 12:54
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jenningsb2/2d94bc286b5fafe878c9e493a0286298 to your computer and use it in GitHub Desktop.
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]].
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
Copy link

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?

Traceback (most recent call last):
  File "c:\Users\Dave Prout\github_temp\roam-obsidian-date-convert.py", line 17, in <module>
    contents = f.read() #reads the contents
  File "C:\Python39\lib\codecs.py", line 322, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x92 in position 644: invalid start byte

@cardei9
Copy link

cardei9 commented Jan 16, 2023

@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