使用csvkit将json转换为csv时出错

时间:2016-03-10 15:04:57

标签: python json csv csvkit

您好我已经将csvkit下载到我的macbook,一切顺利,按照教程enter link description here但是当我尝试使用此命令转换json文件时

in2csv customers-page-1.json > customers-page-1.csv

这是错误

When converting a JSON document with a top-level dictionary element, a key must be specified.

我不知道该怎么把把钥匙放在哪里,因为错误是我的json文件所带来的

{

"page":1,
"pages":132,
"count":5945,
"items":[
    {
        "id":74798241,
        "firstName":"Edyth",
        "lastName":"U.",
        "fullName":"Edyth U.",
        "photoUrl":null,
        "photoType":null,
        "gender":"unknown",
        "age":null,
        "organization":null,
        "jobTitle":null,
        "location":null,
        "createdAt":"2016-03-02T04:26:52Z",
        "modifiedAt":"2016-03-02T04:26:52Z"
    }]}

3 个答案:

答案 0 :(得分:2)

您应该指定要在CSV行中创建列表对象的键。在这种特殊情况下,items

in2csv -k items test.json

作为一般提示,如果你运行一个没有参数的命令,你会得到短暂的使用,而--help切换时会更长:

% in2csv
usage: in2csv [-h] [-d DELIMITER] [-t] [-q QUOTECHAR] [-u {0,1,2,3}] [-b]
              [-p ESCAPECHAR] [-z MAXFIELDSIZE] [-e ENCODING] [-S] [-H] [-v]
              [-l] [--zero] [-f FILETYPE] [-s SCHEMA] [-k KEY] [-y SNIFFLIMIT]
              [--sheet SHEET] [--no-inference]
              [FILE]
in2csv: error: You must specify a format when providing data via STDIN (pipe).

% in2csv --help
usage: in2csv [-h] [-d DELIMITER] [-t] [-q QUOTECHAR] [-u {0,1,2,3}] [-b]
              [-p ESCAPECHAR] [-z MAXFIELDSIZE] [-e ENCODING] [-S] [-H] [-v]
              [-l] [--zero] [-f FILETYPE] [-s SCHEMA] [-k KEY] [-y SNIFFLIMIT]
              [--sheet SHEET] [--no-inference]
              [FILE]

Convert common, but less awesome, tabular data formats to CSV.
....
  -k KEY, --key KEY     Specifies a top-level key to use look within for a
                        list of objects to be converted when processing JSON.

答案 1 :(得分:1)

如此调用程序:

$ in2csv --key items customers-page-1.json > customer.csv

产地:

$ cat customer.csv 
id,firstName,lastName,fullName,photoUrl,photoType,gender,age,organization,jobTitle,location,createdAt,modifiedAt
74798241,Edyth,U.,Edyth U.,,,unknown,,,,,2016-03-02T04:26:52Z,2016-03-02T04:26:52Z

答案 2 :(得分:1)

我得到的答案是代码

std::function

其中 KEY