https://github.com/mechatroner/atom-rainbow-csv
:rainbow: Atom package: Highlight CSV and TSV spreadsheet files in different rainbow colors
https://github.com/mechatroner/atom-rainbow-csv
atom-package csv highlighting tsv
Last synced: 3 months ago
JSON representation
:rainbow: Atom package: Highlight CSV and TSV spreadsheet files in different rainbow colors
- Host: GitHub
- URL: https://github.com/mechatroner/atom-rainbow-csv
- Owner: mechatroner
- License: mit
- Archived: true
- Created: 2018-01-11T05:36:21.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2020-03-30T02:22:50.000Z (over 5 years ago)
- Last Synced: 2024-11-25T03:32:04.748Z (10 months ago)
- Topics: atom-package, csv, highlighting, tsv
- Language: Python
- Homepage:
- Size: 322 KB
- Stars: 16
- Watchers: 3
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README

# Rainbow CSV
### Main features
* Highlight columns in *.csv and *.tsv and other table files in different rainbow colors
* Provide info about the current column in status bar
* Run queries in SQL-like RBQL language
### Usage
Rainbow CSV has content-based CSV/TSV autodetection mechanism enabled by default. This means that package will analyze plain text files even if they do not have "*.csv" or "*.tsv" extension. You can disable content-based autodetection mechanism at the package settings page.Rainbow highlighting can also be manually enabled from Atom context menu:
1. Select a character that you want to use as a delimiter with mouse. Delimiter can be any ASCII symbol, e.g. semicolon
2. Right mouse click: context menu -> Rainbow CSV -> Set as separator ...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 temporary rainbow-highlight even non-table files.Highlighting colors can be adjusted in package settings.
To Run RBQL query select "Rainbow CSV -> RBQL" from Atom context menu or click "RBQL" button at the status panel or run "rbql" command.
By default RBQL uses JavaScript backend, but it can be changed to Python in package settings.#### Difference between "Standard" and "Simple" dialects
When manually enabling rainbow highlighting from the context menu, you have to choose between "Standard" and "Simple" dialects.
* __Standard dialect__ will treat quoted separator as a single field. E.g. line `sell,"15,128",23%` will be treated as 3 columns, because the second comma is quoted. This dialect is used by Excel.
* __Simple dialect__ doesn't care about double quotes: the number of highlighted fields is always N + 1 where N is the number of separators.# RBQL (Rainbow Query Language) Description
RBQL is a technology for (not only) CSV files processing. It provides SQL-like language that supports SELECT queries with Python or JavaScript expressions.
RBQL is distributed with CLI apps, text editor plugins, Python and JS libraries and can work in web browsers.
RBQL core module is very generic and can process all kind of objects and record formats, but most popular RBQL implementation works with CSV files.[Official Site](https://rbql.org/)
### Main Features
* Use Python or JavaScript expressions inside _SELECT_, _UPDATE_, _WHERE_ and _ORDER BY_ statements
* Result set of any query immediately becomes a first-class table on it's own
* Supports input tables with inconsistent number of fields per record
* Output records appear in the same order as in input unless _ORDER BY_ is provided
* Each record has a unique NR (record number) identifier
* Supports all main SQL keywords
* Supports aggregate functions and GROUP BY queries
* Provides some new useful query modes which traditional SQL engines do not have
* Supports both _TOP_ and _LIMIT_ keywords
* Supports user-defined functions (UDF)
* Works out of the box, no external dependencies#### 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_All keywords have the same meaning as in SQL queries. You can check them [online](https://www.w3schools.com/sql/default.asp)
### 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 first (header) CSV line 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 first (header) CSV line, even when there is no header at all#### Notes:
* You can mix all variable types in a single query, example:
```select a1, b2 JOIN /path/to/b.csv ON a['Item Id'] == b.Identifier WHERE NR > 1 and int(a.Weight) * 100 > int(b["weight of the item"])```
* Referencing fields by header names does not automatically skip the header line (you can use `where NR > 1` trick to skip it)
* If you want to use RBQL as a library for your own app you can define your own custom variables and do not have to support the above mentioned CSV-related variables.### 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. This prevents accidental data loss from poorly written queries.
_UPDATE SET_ is synonym to _UPDATE_, because in RBQL there is no need to specify the source table.### 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 it's file path or by it's name - an arbitary string which 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 left table "A" doesn't have exactly one matching key in the right table "B".
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')`### 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 * order by random.random() where NR > 1` - skip header record and random sort
* `select len(a.vehicle_price) / 10, a2 where NR > 1 and a['Vehicle type'] in ["car", "plane", "boat"] limit 20` - referencing columns by names from header record, skipping the 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#### With JavaScript expressions
* `select top 100 a1, a2 * 10, a4.length where a1 == "Buy" order by parseInt(a2) desc`
* `select * order by Math.random() where NR > 1` - skip header record and random sort
* `select top 20 a.vehicle_price.length / 10, a2 where NR > 1 and ["car", "plane", "boat"].indexOf(a['Vehicle type']) > -1 limit 20` - referencing columns by names from header record and skipping the 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### FAQ
#### How do I skip header record in CSV files?
You can use the following trick: add `... where NR > 1 ...` to your query
And if you are doing math operation you can modify your query like this, example:
`select int(a3) * 1000, a2` -> `select int(a3) * 1000 if NR > 1 else a3, a2`## References
* rainbow_csv plugin in [Vim](https://github.com/mechatroner/rainbow_csv)
* Rainbow CSV extension in [Visual Studio Code](https://marketplace.visualstudio.com/items?itemName=mechatroner.rainbow-csv)
* rainbow_csv plugin in [Sublime Text](https://packagecontrol.io/packages/rainbow_csv)
* rainbow_csv plugin in [gedit](https://github.com/mechatroner/gtk_gedit_rainbow_csv) - doesn't support quoted commas in csv
* rainbow_csv_4_nedit in [NEdit](https://github.com/DmitTrix/rainbow_csv_4_nedit)
* CSV highlighting in [Nano](https://github.com/scopatz/nanorc)
* Rainbow CSV in [IntelliJ IDEA](https://plugins.jetbrains.com/plugin/12896-rainbow-csv/)
* RBQL [Official Site](https://rbql.org/)