Skip to content

Instantly share code, notes, and snippets.

@davidgilbertson
Last active August 23, 2023 20:37
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 davidgilbertson/8b15fb6ba93476dc28c1ca0bcace28a8 to your computer and use it in GitHub Desktop.
Save davidgilbertson/8b15fb6ba93476dc28c1ca0bcace28a8 to your computer and use it in GitHub Desktop.
Custom extractor for PyCharm - copy tables to a list of dicts
import static com.intellij.openapi.util.text.StringUtil.escapeStringCharacters as escapeStr
SEPARATOR = ", "
QUOTE = "\""
NEWLINE = System.getProperty("line.separator")
def record(columns, dataRow) {
OUT.append(" {").append(NEWLINE)
columns.eachWithIndex { column, idx ->
def skipQuote = dataRow.value(column).toString().isNumber() || dataRow.value(column) == null
def stringValue = dataRow.value(column) == null ? "None" : FORMATTER.format(dataRow, column)
OUT.append(" ")
.append(QUOTE).append(column.name()).append(QUOTE)
.append(": ")
.append(skipQuote ? "": QUOTE).append(escapeStr(stringValue)).append(skipQuote ? "": QUOTE)
.append(SEPARATOR + NEWLINE)
}
OUT.append(" },").append(NEWLINE)
}
OUT.append("data = [").append(NEWLINE)
ROWS.each { row -> record(COLUMNS, row) }
OUT.append("]")
@davidgilbertson
Copy link
Author

Docs for 'installing' this extractor: https://www.jetbrains.com/help/pycharm/data-extractors.html#creating-any-text-extractor-with

You can then copy a table (left) and paste it as valid Python (right):
image

Copied from https://github.com/aoiaoi/jetbrains-intellij-extractors/blob/master/src/Python-Dictionaries.py.groovy with handling for None and quotes added.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment