Skip to content

Instantly share code, notes, and snippets.

@ajkerrigan
Last active December 29, 2022 21:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ajkerrigan/bd9cb288d94ab3c080465691f9b0fd52 to your computer and use it in GitHub Desktop.
Save ajkerrigan/bd9cb288d94ab3c080465691f9b0fd52 to your computer and use it in GitHub Desktop.
Hanukkah of Data 2022 - VisiData Snippets
#!vd -p
{"longname": "open-file", "input": "noahs-customers.csv", "keystrokes": "o"}
{"sheet": "noahs-customers", "col": "name", "row": "", "longname": "phone-keys-for-col", "input": "", "comment": ""}
{"sheet": "noahs-customers", "col": "", "row": "", "longname": "select-expr", "input": "phone.replace('-','') in name_phonekeys", "keystrokes": "z|", "comment": "select rows matching Python expression in any visible column"}
{"sheet": "noahs-customers", "col": "", "row": "", "longname": "dup-selected", "input": "", "keystrokes": "\"", "comment": "open duplicate sheet with only selected rows"}
from visidata import vd, TableSheet, ExprColumn
phone_pad = {
"abc": 2,
"def": 3,
"ghi": 4,
"jkl": 5,
"mno": 6,
"pqrs": 7,
"tuv": 8,
"wxyz": 9,
}
vd.phone_lookup = str.maketrans(
{letter: str(number) for letters, number in phone_pad.items() for letter in letters}
)
@TableSheet.api
def phoneKeysFor(sheet, col):
newcol = ExprColumn("curcol.lower().replace(' ','').translate(vd.phone_lookup)", curcol=col)
newcol.name = f"{col.name}_phonekeys"
sheet.addColumnAtCursor(newcol)
TableSheet.addCommand("", "phone-keys-for-col", "sheet.phoneKeysFor(cursorCol)")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment