Last active
August 23, 2023 20:37
-
-
Save davidgilbertson/8b15fb6ba93476dc28c1ca0bcace28a8 to your computer and use it in GitHub Desktop.
Custom extractor for PyCharm - copy tables to a list of dicts
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 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("]") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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):
Copied from https://github.com/aoiaoi/jetbrains-intellij-extractors/blob/master/src/Python-Dictionaries.py.groovy with handling for
None
and quotes added.