ctrl+shift+p filters: :st2 :st3 :win :osx :linux
Browse

rainbow_csv

by mechatroner ST3

šŸŒˆRainbow CSV - Sublime Text Package: Highlight columns in CSV and TSV files and run queeries in SQL-like language

Labels csv, tsv, highlight

Details

Installs

  • Total 45K
  • Win 20K
  • Mac 18K
  • Linux 7K
May 12 May 11 May 10 May 9 May 8 May 7 May 6 May 5 May 4 May 3 May 2 May 1 Apr 30 Apr 29 Apr 28 Apr 27 Apr 26 Apr 25 Apr 24 Apr 23 Apr 22 Apr 21 Apr 20 Apr 19 Apr 18 Apr 17 Apr 16 Apr 15 Apr 14 Apr 13 Apr 12 Apr 11 Apr 10 Apr 9 Apr 8 Apr 7 Apr 6 Apr 5 Apr 4 Apr 3 Apr 2 Apr 1 Mar 31 Mar 30 Mar 29 Mar 28
Windows 6 7 11 10 8 12 16 2 2 12 11 13 16 13 8 10 10 16 17 10 10 7 3 13 22 21 19 15 6 3 15 16 7 14 14 5 12 10 13 12 14 13 4 7 12 10
Mac 5 1 4 17 11 13 12 2 3 10 6 7 6 4 4 8 8 15 14 14 15 2 4 12 15 9 12 17 10 5 21 10 11 15 11 3 7 9 8 9 10 10 3 6 11 6
Linux 2 2 7 2 5 8 1 3 1 4 4 4 3 7 4 3 3 5 10 5 7 3 1 0 4 4 4 4 4 2 3 4 9 4 9 15 4 8 9 3 7 4 5 1 2 1

Readme

Source
raw.ā€‹githubusercontent.ā€‹com

Rainbow CSV

Main features

  • Highlight columns in *.csv, *.tsv and other separated files in different rainbow colors.
  • Provide info about columns on mouse hover.
  • Check consistency of CSV files (CSVLint)
  • Align columns with spaces and Shrink (trim spaces from fields)
  • Execute SQL-like RBQL queries.

Usage

Rainbow CSV has content-based csv/tsv autodetection mechanism. This means that the package will analyze plain text files even if they do not have ā€œ.csvā€ or ā€œ.tsvā€ extension.

Rainbow highlighting can also be manually enabled from Sublime context menu (see the demo gif below):
1. Select a character (or sequence of characters) that you want to use as a delimiter with the cursor 2. Right mouse click: context menu -> Rainbow CSV -> Enable ā€¦

You can also disable rainbow highlighting and go back to the original file highlighting using the same context menu.
This feature can be used to temporarily rainbow-highlight even non-table files.

Manual Rainbow Enabling/Disabling demo gif:

Rainbow CSV also lets you execute SQL-like queries in RBQL language, see the demo gif below:

To Run RBQL query press F5 or select ā€œRainbow CSVā€ -> ā€œRun RBQL queryā€ option from the file context menu.

Key mappings

Key Action
F5 Start query editing for the current CSV file

Commands

Rainbow CSV: Enable Simple

Before running the command you need to select the separator (single or multiple characters) with your cursor Set the selected character as the separator and enables syntax highlighting. Sublime will generate the syntax file if it doesn't exist. Simple dialect completely ignores double quotes: i.e. separators can not be escaped in double quoted fields

Rainbow CSV: Enable Standard

Same as the Enable Simple command, but separators can be escaped in double quoted fields.

Rainbow CSV: CSVLint

The linter checks the following:
* consistency of double quotes usage in CSV rows
* consistency of number of fields per CSV row

Rainbow CSV: Run RBQL query

Run RBQL query for the current file.
Unlike F5 button it will work even if the current file is not a CSV table: in this case only 2 variables ā€œa1ā€ and ā€œNRā€ will be available.

Rainbow CSV: Align CSV columns with spaces

Align CSV columns with spaces in the current file

Rainbow CSV: Shrink CSV table

Remove leading and trailing spaces from all fields in the current file

Configuration

To adjust plugin configuration:
1. Go to ā€œPreferencesā€ -> ā€œPackage Settingsā€ -> ā€œRainbow CSVā€ -> ā€œSettingsā€.
2. On the right side change the settings like you'd like.

Configuration parameters

To configure the extension, click ā€œPreferencesā€ -> ā€œPackage Settingsā€ -> ā€œRainbow CSVā€ -> ā€œSettingsā€

ā€œallow_newlines_in_fieldsā€

Allow quoted multiline fields as defined in RFC-4180

ā€œenable_rainbow_csv_autodetectā€

Enable content-based separator autodetection. Files with ā€œ.csvā€ and ā€œ.tsvā€ extensions are always highlighted no matter what is the value of this option.

ā€œrainbow_csv_autodetect_dialectsā€

List of CSV dialects to autodetect.
If ā€œenable_rainbow_csv_autodetectā€ is set to false this setting is ignored

ā€œrainbow_csv_max_file_size_bytesā€

Disable Rainbow CSV for files bigger than the specified size. This can be helpful to prevent poor performance and crashes with very large files.
Manual separator selection will override this setting for the current file.
E.g. to disable on files larger than 100 MB, set "rainbow_csv_max_file_size_bytes": 100000000

ā€œuse_custom_rainbow_colorsā€

Use custom high-contrast rainbow colors instead of colors provided by your current color scheme. When you enable this option, ā€œauto_adjust_rainbow_colorsā€ also gets enabled by default.

ā€œauto_adjust_rainbow_colorsā€

Auto adjust rainbow colors for Packages/User/RainbowCSV.sublime-color-scheme
Rainbow CSV will auto-generate color theme with high-contrast colors to make CSV columns more distinguishable.
You can disable this setting and manually customize Rainbow CSV color scheme at Packages/User/RainbowCSV.sublime-color-scheme, you can use the following RainbowCSV.sublime-color-scheme file as a starting point for your customizations. Do NOT manually customize Packages/User/RainbowCSV.sublime-color-scheme without disabling this setting, the plugin will just rewrite it in that case. This option has effect only if ā€œuse_custom_rainbow_colorsā€ is set to true

ā€œrbql_backend_languageā€

RBQL backend language.
Supported values: ā€œPythonā€, ā€œJSā€
To use RBQL with JavaScript (JS) you need to have Node JS installed and added to your system path.

ā€œrbql_with_headersā€

Default: false RBQL will treat first records in all input and join files as headers.
You can set this value to true if most of the CSV files you deal with have headers.
You can override this setting on the query level by either adding WITH (header) or WITH (noheader) to the end of the query.

ā€œrbql_output_formatā€

Format of RBQL result set tables.
Supported values: ā€œtsvā€, ā€œcsvā€, ā€œinputā€
* input: same format as the input table * tsv: tab separated values. * csv: is Excel-compatible and allows quoted commas.

Example: to always use ā€œtsvā€ as output format add this line to your settings file: "rbql_output_format": "tsv",

ā€œrbql_encodingā€

RBQL encoding for files and queries.
Supported values: ā€œlatin-1ā€, ā€œutf-8ā€

References

  • This Sublime Text plugin is an adaptation of Vim's rainbow_csv plugin

RBQL (Rainbow Query Language) Description

RBQL is an eval-based SQL-like query engine for (not only) CSV file processing. It provides SQL-like language that supports SELECT queries with Python or JavaScript expressions.
RBQL is best suited for data transformation, data cleaning, and analytical queries.
RBQL is distributed with CLI apps, text editor plugins, Python and JS libraries.

Official Site

Main Features

  • Use Python or JavaScript expressions inside SELECT, UPDATE, WHERE and ORDER BY statements
  • Supports multiple input formats
  • Result set of any query immediately becomes a first-class table on its own
  • No need to provide FROM statement in the query when the input table is defined by the current context.
  • Supports all main SQL keywords
  • Supports aggregate functions and GROUP BY queries
  • Supports user-defined functions (UDF)
  • Provides some new useful query modes which traditional SQL engines do not have
  • Lightweight, dependency-free, works out of the box

Limitations:

  • RBQL doesn't support nested queries, but they can be emulated with consecutive queries
  • Number of tables in all JOIN queries is always 2 (input table and join table), use consecutive queries to join 3 or more tables

Supported SQL Keywords (Keywords are case insensitive)

  • SELECT
  • UPDATE
  • WHERE
  • ORDER BY ā€¦ [ DESC | ASC ]
  • [ LEFT | INNER ] JOIN
  • DISTINCT
  • GROUP BY
  • TOP N
  • LIMIT N
  • AS

All keywords have the same meaning as in SQL queries. You can check them online

RBQL variables

RBQL for CSV files provides the following variables which you can use in your queries:

  • a1, a2,ā€¦, a{N}
    Variable type: string
    Description: value of i-th field in the current record in input table
  • b1, b2,ā€¦, b{N}
    Variable type: string
    Description: value of i-th field in the current record in join table B
  • NR
    Variable type: integer
    Description: Record number (1-based)
  • NF
    Variable type: integer
    Description: Number of fields in the current record
  • a.name, b.Person_age, ā€¦ a.{Good_alphanumeric_column_name}
    Variable type: string
    Description: Value of the field referenced by it's ā€œnameā€. You can use this notation if the field in the header has a ā€œgoodā€ alphanumeric name
  • a[ā€œobject idā€], a['9.12341234'], b[ā€œ%$ !! 10 20ā€] ā€¦ a[ā€œArbitrary column name!ā€]
    Variable type: string
    Description: Value of the field referenced by it's ā€œnameā€. You can use this notation to reference fields by arbitrary values in the header

UPDATE statement

UPDATE query produces a new table where original values are replaced according to the UPDATE expression, so it can also be considered a special type of SELECT query.

Aggregate functions and queries

RBQL supports the following aggregate functions, which can also be used with GROUP BY keyword:
COUNT, ARRAY_AGG, MIN, MAX, SUM, AVG, VARIANCE, MEDIAN

Limitation: aggregate functions inside Python (or JS) expressions are not supported. Although you can use expressions inside aggregate functions.
E.g. MAX(float(a1) / 1000) - valid; MAX(a1) / 1000 - invalid.
There is a workaround for the limitation above for ARRAY_AGG function which supports an optional parameter - a callback function that can do something with the aggregated array. Example:
SELECT a2, ARRAY_AGG(a1, lambda v: sorted(v)[:5]) GROUP BY a2 - Python; SELECT a2, ARRAY_AGG(a1, v => v.sort().slice(0, 5)) GROUP BY a2 - JS

JOIN statements

Join table B can be referenced either by its file path or by its name - an arbitrary string which the user should provide before executing the JOIN query.
RBQL supports STRICT LEFT JOIN which is like LEFT JOIN, but generates an error if any key in the left table ā€œAā€ doesn't have exactly one matching key in the right table ā€œBā€.
Table B path can be either relative to the working dir, relative to the main table or absolute.
Limitation: JOIN statements can't contain Python/JS expressions and must have the following form: (/path/to/table.tsv | table_name ) ON aā€¦ == bā€¦ [AND aā€¦ == bā€¦ [AND ā€¦ ]]

SELECT EXCEPT statement

SELECT EXCEPT can be used to select everything except specific columns. E.g. to select everything but columns 2 and 4, run: SELECT * EXCEPT a2, a4
Traditional SQL engines do not support this query mode.

UNNEST() operator

UNNEST(list) takes a list/array as an argument and repeats the output record multiple times - one time for each value from the list argument.
Example: SELECT a1, UNNEST(a2.split(';'))

LIKE() function

RBQL does not support LIKE operator, instead it provides ā€œlike()ā€ function which can be used like this: SELECT * where like(a1, 'foo%bar')

WITH (header) and WITH (noheader) statements

You can set whether the input (and join) CSV file has a header or not using the environment configuration parameters which could be --with_headers CLI flag or GUI checkbox or something else. But it is also possible to override this selection directly in the query by adding either WITH (header) or WITH (noheader) statement at the end of the query. Example: select top 5 NR, * with (header)

User Defined Functions (UDF)

RBQL supports User Defined Functions
You can define custom functions and/or import libraries in two special files:
* ~/.rbql_init_source.py - for Python * ~/.rbql_init_source.js - for JavaScript

Examples of RBQL queries

With Python expressions

  • SELECT TOP 100 a1, int(a2) * 10, len(a4) WHERE a1 == "Buy" ORDER BY int(a2) DESC
  • SELECT a.id, a.weight / 1000 AS weight_kg
  • SELECT * ORDER BY random.random() - random sort
  • SELECT len(a.vehicle_price) / 10, a2 WHERE int(a.vehicle_price) < 500 and a['Vehicle type'] in ["car", "plane", "boat"] limit 20 - referencing columns by names from header and using Python's ā€œinā€ to emulate SQL's ā€œinā€
  • UPDATE SET a3 = 'NPC' WHERE a3.find('Non-playable character') != -1
  • SELECT NR, * - enumerate records, NR is 1-based
  • SELECT * WHERE re.match(".*ab.*", a1) is not None - select entries where first column has ā€œabā€ pattern
  • SELECT a1, b1, b2 INNER JOIN ./countries.txt ON a2 == b1 ORDER BY a1, a3 - example of join query
  • SELECT MAX(a1), MIN(a1) WHERE a.Name != 'John' GROUP BY a2, a3 - example of aggregate query
  • SELECT *a1.split(':') - Using Python3 unpack operator to split one column into many. Do not try this with other SQL engines!

With JavaScript expressions

  • SELECT TOP 100 a1, a2 * 10, a4.length WHERE a1 == "Buy" ORDER BY parseInt(a2) DESC
  • SELECT a.id, a.weight / 1000 AS weight_kg
  • SELECT * ORDER BY Math.random() - random sort
  • SELECT TOP 20 a.vehicle_price.length / 10, a2 WHERE parseInt(a.vehicle_price) < 500 && ["car", "plane", "boat"].indexOf(a['Vehicle type']) > -1 limit 20 - referencing columns by names from header
  • UPDATE SET a3 = 'NPC' WHERE a3.indexOf('Non-playable character') != -1
  • SELECT NR, * - enumerate records, NR is 1-based
  • SELECT a1, b1, b2 INNER JOIN ./countries.txt ON a2 == b1 ORDER BY a1, a3 - example of join query
  • SELECT MAX(a1), MIN(a1) WHERE a.Name != 'John' GROUP BY a2, a3 - example of aggregate query
  • SELECT ...a1.split(':') - Using JS ā€œdestructuring assignmentā€ syntax to split one column into many. Do not try this with other SQL engines!

References

Rainbow CSV and similar plugins in other editors:

RBQL: