使用方法
1 | master ✗ $ python csv2json.py --help |
如果执行成功,命令行输出会像下面示例一样。
1 | master ✗ $ python csv2json.py --csv_file ./test.csv --json_file ./yiran-test.json |
如果执行失败,则会提示具体失败原因。如:csv 文件无法找到。
1 | master ✗ $ python csv2json.py --csv_file ./tes.csv --json_file ./yiran-test.json |
转换效果
test.csv1
2
3
4
5name,age (years),weight (kg),birth day,birth month,birth year,adopted_a,adopted_since,adopted_finish
Tommy,5,3.6,11,April,2011,TRUE,2012,2015
Clara,2,8.2,6,May,2015,FALSE,N/A,N/A
Catnip,6,3.3,21,August,2011,TRUE,2017,2020
Ciel,3,3.1,18,January,2015,TRUE,2018,2021
test.json1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62[
{
"weight (kg)": "3.6",
"birth": {
"year": "2011",
"day": "11",
"month": "April"
},
"name": "Tommy",
"adopted": {
"a": "TRUE",
"since": "2012",
"finish": "2015"
},
"age (years)": "5"
},
{
"weight (kg)": "8.2",
"birth": {
"year": "2015",
"day": "6",
"month": "May"
},
"name": "Clara",
"adopted": {
"a": "FALSE",
"since": "N/A",
"finish": "N/A"
},
"age (years)": "2"
},
{
"weight (kg)": "3.3",
"birth": {
"year": "2011",
"day": "21",
"month": "August"
},
"name": "Catnip",
"adopted": {
"a": "TRUE",
"since": "2017",
"finish": "2020"
},
"age (years)": "6"
},
{
"weight (kg)": "3.1",
"birth": {
"year": "2015",
"day": "18",
"month": "January"
},
"name": "Ciel",
"adopted": {
"a": "TRUE",
"since": "2018",
"finish": "2021"
},
"age (years)": "3"
}
]
具体实现
1 | #!/usr/bin/python |