Comandos avanzados#

Preparación#

[1]:
!mkdir -p /tmp/csvkit_tutorial
%cd /tmp/csvkit_tutorial
!curl -L -O --silent https://raw.githubusercontent.com/wireservice/csvkit/master/examples/realdata/ne_1033_data.xlsx
!curl -L -O --silent https://raw.githubusercontent.com/wireservice/csvkit/master/examples/realdata/acs2012_5yr_population.csv
!in2csv ne_1033_data.xlsx > data.csv
/tmp/csvkit_tutorial

csvjson#

[2]:
!csvcut -c county,item_name data.csv | csvgrep -c county -m "GREELEY" | csvjson --indent 4
[
    {
        "county": "GREELEY",
        "item_name": "RIFLE,7.62 MILLIMETER"
    },
    {
        "county": "GREELEY",
        "item_name": "RIFLE,7.62 MILLIMETER"
    },
    {
        "county": "GREELEY",
        "item_name": "RIFLE,7.62 MILLIMETER"
    }
]
[3]:
!csvjson --indent 4 --key fips acs2012_5yr_population.csv | head
{
    "31001": {
        "fips": 31001.0,
        "name": "Adams County, NE",
        "total_population": 31299.0,
        "margin_of_error": 0.0
    },
    "31003": {
        "fips": 31003.0,
        "name": "Antelope County, NE",

csvpy#

$ csvpy data.csv
Welcome! "data.csv" has been loaded in a reader object named "reader".
>>> print(len(list(reader)))
1037

csvformat#

[4]:
!csvformat -D \| data.csv | head
state|county|fips|nsn|item_name|quantity|ui|acquisition_cost|total_cost|ship_date|federal_supply_category|federal_supply_category_name|federal_supply_class|federal_supply_class_name
NE|ADAMS|31001.0|1005-00-589-1271|RIFLE,7.62 MILLIMETER|1.0|Each|138.0|138.0|2008-07-11|10.0|WEAPONS|1005.0|Guns, through 30 mm
NE|ADAMS|31001.0|1005-00-589-1271|RIFLE,7.62 MILLIMETER|1.0|Each|138.0|138.0|2008-07-11|10.0|WEAPONS|1005.0|Guns, through 30 mm
NE|ADAMS|31001.0|1005-00-589-1271|RIFLE,7.62 MILLIMETER|1.0|Each|138.0|138.0|2008-07-11|10.0|WEAPONS|1005.0|Guns, through 30 mm
NE|ADAMS|31001.0|1005-00-589-1271|RIFLE,7.62 MILLIMETER|1.0|Each|138.0|138.0|2008-07-11|10.0|WEAPONS|1005.0|Guns, through 30 mm
NE|ADAMS|31001.0|1005-00-589-1271|RIFLE,7.62 MILLIMETER|1.0|Each|138.0|138.0|2008-07-11|10.0|WEAPONS|1005.0|Guns, through 30 mm
NE|ADAMS|31001.0|1005-00-589-1271|RIFLE,7.62 MILLIMETER|1.0|Each|138.0|138.0|2008-07-11|10.0|WEAPONS|1005.0|Guns, through 30 mm
NE|BUFFALO|31019.0|1005-00-073-9421|RIFLE,5.56 MILLIMETER|1.0|Each|499.0|499.0|2008-09-24|10.0|WEAPONS|1005.0|Guns, through 30 mm
NE|BUFFALO|31019.0|1005-00-073-9421|RIFLE,5.56 MILLIMETER|1.0|Each|499.0|499.0|2008-09-24|10.0|WEAPONS|1005.0|Guns, through 30 mm
NE|BUFFALO|31019.0|1005-00-073-9421|RIFLE,5.56 MILLIMETER|1.0|Each|499.0|499.0|2008-09-24|10.0|WEAPONS|1005.0|Guns, through 30 mm
[5]:
!csvformat -T data.csv | head
state   county  fips    nsn     item_name       quantity        ui      acquisition_cost        total_cost      ship_date       federal_supply_category federal_supply_category_name    federal_supply_class    federal_supply_class_name
NE      ADAMS   31001.0 1005-00-589-1271        RIFLE,7.62 MILLIMETER   1.0     Each    138.0   138.0   2008-07-11      10.0    WEAPONS 1005.0  Guns, through 30 mm
NE      ADAMS   31001.0 1005-00-589-1271        RIFLE,7.62 MILLIMETER   1.0     Each    138.0   138.0   2008-07-11      10.0    WEAPONS 1005.0  Guns, through 30 mm
NE      ADAMS   31001.0 1005-00-589-1271        RIFLE,7.62 MILLIMETER   1.0     Each    138.0   138.0   2008-07-11      10.0    WEAPONS 1005.0  Guns, through 30 mm
NE      ADAMS   31001.0 1005-00-589-1271        RIFLE,7.62 MILLIMETER   1.0     Each    138.0   138.0   2008-07-11      10.0    WEAPONS 1005.0  Guns, through 30 mm
NE      ADAMS   31001.0 1005-00-589-1271        RIFLE,7.62 MILLIMETER   1.0     Each    138.0   138.0   2008-07-11      10.0    WEAPONS 1005.0  Guns, through 30 mm
NE      ADAMS   31001.0 1005-00-589-1271        RIFLE,7.62 MILLIMETER   1.0     Each    138.0   138.0   2008-07-11      10.0    WEAPONS 1005.0  Guns, through 30 mm
NE      BUFFALO 31019.0 1005-00-073-9421        RIFLE,5.56 MILLIMETER   1.0     Each    499.0   499.0   2008-09-24      10.0    WEAPONS 1005.0  Guns, through 30 mm
NE      BUFFALO 31019.0 1005-00-073-9421        RIFLE,5.56 MILLIMETER   1.0     Each    499.0   499.0   2008-09-24      10.0    WEAPONS 1005.0  Guns, through 30 mm
NE      BUFFALO 31019.0 1005-00-073-9421        RIFLE,5.56 MILLIMETER   1.0     Each    499.0   499.0   2008-09-24      10.0    WEAPONS 1005.0  Guns, through 30 mm
[6]:
!csvformat -U 1 data.csv | head
"state","county","fips","nsn","item_name","quantity","ui","acquisition_cost","total_cost","ship_date","federal_supply_category","federal_supply_category_name","federal_supply_class","federal_supply_class_name"
"NE","ADAMS","31001.0","1005-00-589-1271","RIFLE,7.62 MILLIMETER","1.0","Each","138.0","138.0","2008-07-11","10.0","WEAPONS","1005.0","Guns, through 30 mm"
"NE","ADAMS","31001.0","1005-00-589-1271","RIFLE,7.62 MILLIMETER","1.0","Each","138.0","138.0","2008-07-11","10.0","WEAPONS","1005.0","Guns, through 30 mm"
"NE","ADAMS","31001.0","1005-00-589-1271","RIFLE,7.62 MILLIMETER","1.0","Each","138.0","138.0","2008-07-11","10.0","WEAPONS","1005.0","Guns, through 30 mm"
"NE","ADAMS","31001.0","1005-00-589-1271","RIFLE,7.62 MILLIMETER","1.0","Each","138.0","138.0","2008-07-11","10.0","WEAPONS","1005.0","Guns, through 30 mm"
"NE","ADAMS","31001.0","1005-00-589-1271","RIFLE,7.62 MILLIMETER","1.0","Each","138.0","138.0","2008-07-11","10.0","WEAPONS","1005.0","Guns, through 30 mm"
"NE","ADAMS","31001.0","1005-00-589-1271","RIFLE,7.62 MILLIMETER","1.0","Each","138.0","138.0","2008-07-11","10.0","WEAPONS","1005.0","Guns, through 30 mm"
"NE","BUFFALO","31019.0","1005-00-073-9421","RIFLE,5.56 MILLIMETER","1.0","Each","499.0","499.0","2008-09-24","10.0","WEAPONS","1005.0","Guns, through 30 mm"
"NE","BUFFALO","31019.0","1005-00-073-9421","RIFLE,5.56 MILLIMETER","1.0","Each","499.0","499.0","2008-09-24","10.0","WEAPONS","1005.0","Guns, through 30 mm"
"NE","BUFFALO","31019.0","1005-00-073-9421","RIFLE,5.56 MILLIMETER","1.0","Each","499.0","499.0","2008-09-24","10.0","WEAPONS","1005.0","Guns, through 30 mm"