Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
6
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Switch to GitLab Next
Sign in / Register
Toggle navigation
A
attic
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Iterations
Merge Requests
0
Merge Requests
0
Requirements
Requirements
List
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Operations
Operations
Incidents
Packages & Registries
Packages & Registries
Container Registry
Analytics
Analytics
Code Review
Insights
Issue
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
hydrargyrum
attic
Commits
7bb3f5d0
Commit
7bb3f5d0
authored
Oct 15, 2017
by
hydrargyrum
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
csv2json: transform CSV into JSON
parent
a704e71e
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
39 additions
and
0 deletions
+39
-0
csv2json/csv2json
csv2json/csv2json
+39
-0
No files found.
csv2json/csv2json
0 → 100755
View file @
7bb3f5d0
#!/usr/bin/env python3
from
argparse
import
ArgumentParser
from
collections
import
OrderedDict
import
csv
import
json
import
sys
def
sniff
(
fn
):
with
open
(
fn
)
as
fd
:
sn
=
csv
.
Sniffer
()
return
sn
.
sniff
(
fd
.
read
(
1024
))
def
read_rows
(
fd
,
args
):
if
args
.
header
:
for
row
in
csv
.
DictReader
(
fd
,
delimiter
=
args
.
delimiter
):
yield
row
else
:
for
row
in
csv
.
reader
(
fd
,
delimiter
=
args
.
delimiter
):
yield
OrderedDict
((
str
(
n
),
v
)
for
n
,
v
in
enumerate
(
row
))
def
main
():
parser
=
ArgumentParser
()
parser
.
add_argument
(
'--header'
,
action
=
'store_true'
)
parser
.
add_argument
(
'-d'
,
'--delimiter'
,
default
=
','
)
parser
.
add_argument
(
'--sniff'
,
action
=
'store_true'
)
parser
.
add_argument
(
'file'
)
args
=
parser
.
parse_args
()
with
open
(
args
.
file
)
as
fd
:
data
=
list
(
read_rows
(
fd
,
args
))
print
(
json
.
dumps
(
data
))
if
__name__
==
'__main__'
:
main
()
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment