evalcsv

1.1.23 • Public • Published

EvalCsv

EvalCsv是一个可以把csv文件转换为json文件的命令行工具。
csv里面可以填一个对象:
	p
	{x:0,y:1}
还可以填一个数组:
	arr
	[{x:0,y:1}, {x:2,y:3}]
new一个对象也可以:
	m
	new Map()
支持javascript代码,语法简洁,错误提示全面, 因为这个程序仅仅是把csv里面的元素读取出来eval而已……

Solution

对于csv文件中的每一个元素value, 假定key为标题行对应列的值, evalcsv使用了一个很简单的办法来把csv文件转换为json对象:

		eval("row." + key + " = " + value + ";");
        
有木有感觉到用csv表示对象, 数组等复杂数据原来是这么容易的事情?
用node.js的小伙伴们, 直接require这个json文件开始愉快的工作吧。

想要把csv资源打包的朋友们, 可以使用json2pb把json对象用protobuf打包。

Install

  1. Install node.js

  2. Install with npm

    npm install -g evalcsv

Usage

Usage: evalcsv [options] files
Options:
  -h, --help                   Display this information.
  -v, --version                Print the compiler version.
  -e, --encoding               Set input file encoding, default encoding is 'gbk'.
  -k, --key                    Set the line number of keys, default is 2.
  -s, --start                  Set the starting line number for parsing, default is 3.
  -f, --format                 Set the output format, default is {"rows":%s}

Example

motion.csv
    motion_id,name,privileges,type,category
    motion_id,name,privileges,type,category
    11001,"""跪姿肩部下沉""","['owner', 'admin', 'user', 'guest']","""热身""","""胸"""
    11002,"""直体肩部下沉""","['owner', 'admin', 'user', 'guest']","""热身""","""胸"""

command line
    evalcsv motion.csv
    
motion.json
	{
		"rows": [
			{
				"motion_id": 11001,
				"name": "跪姿肩部下沉",
				"privileges": [
					"owner",
					"admin",
					"user",
					"guest"
				],
				"type": "热身",
				"category": "胸",
			},
			{
				"motion_id": 11002,
				"name": "直体肩部下沉",
				"privileges": [
					"owner",
					"admin",
					"user",
					"guest"
				],
				"type": "热身",
				"category": "胸",
			}
		]
	}

Author

@randyliu

Licence

See the MIT License

Package Sidebar

Install

npm i evalcsv

Weekly Downloads

3

Version

1.1.23

License

MIT

Last publish

Collaborators

  • randyliu