Last active
September 10, 2023 14:20
-
-
Save snoyes/405b1e0615113a393ab7bd0d1be9b2d0 to your computer and use it in GitHub Desktop.
Anki: move part of field to different field, or remove unwanted content from a field
This file contains hidden or 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
# Open the debug console with Ctrl+: (which is Ctrl+Shift+; on US keyboards) | |
# (Don't have the browser window open when you do.) | |
# Run it with Ctrl+Enter | |
# Remove the # from the last line to actually save the changes | |
s = r'regular expression to search for goes here' | |
srcField = 'Source Field Name goes here' | |
tgtField = 'Target Field Name goes here' | |
noteType = 'Name of the Note Type goes here' | |
# Get the model, and then the list of notes of that type | |
theModel = mw.col.models.byName(noteType) | |
theNotes = mw.col.models.nids(theModel) | |
# Run through the list of notes | |
for nid in theNotes: | |
# Get the note contents | |
note = mw.col.getNote(nid) | |
# Search the source field for the regexp | |
m = re.search(s, note[srcField]) | |
# If there's a match... | |
if (m): | |
#... show what it found | |
print(m.group(0)) | |
#... add it to the target field (comment out if just clearing unwanted content)... | |
note[tgtField] = m.group(0) | |
#... and remove it from the source field | |
note[srcField] = re.sub(s, '', note[srcField]) | |
# save the changes | |
#note.flush() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment