csvkit

how can I edit the output of csvstat to... csv


How can I edit the output of csvkit's csvstat tool so it is formatted as... CSV?

More specifically, csvstat can output counts & tabulations from the contents of CSV files. For example, the following command will read a file named data.csv and then count & tabulate the values in the column named "pos":

csvstat -c pos data.csv --freq

The result is a string looking like a Python dictionary:

{ "NNP": 2204, "NN": 2035, "IN": 1512, ",": 1039, "NNS": 1006 }

Do you know of a quick & easy way to transform this string into CSV? A one-liner? Maybe through the use of another csvkit tool?


Solution

  • Using csvkit and jq, you can run

    csvstat -c pos data.csv --freq | jq -s . | in2csv -I -f json