FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
WranglerScripts/JSON2TSV.py at master · ENCODE-DCC/WranglerScripts · GitHub
ENCODE-DCC
/
WranglerScripts
Public
Notifications
You must be signed in to change notification settings
Fork
4
Star
5
Code
Issues
0
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
WranglerScripts
/
JSON2TSV.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
44 lines (35 loc) · 1.07 KB
Breadcrumbs
WranglerScripts
/
JSON2TSV.py
Copy path
File metadata and controls
44 lines (35 loc) · 1.07 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
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
#!/usr/bin/env python
# -*- coding: latin-1 -*-
''' Script to convert a JSON file to TSV. Adapted from http://kailaspatil.blogspot.com/2013/07/python-script-to-convert-json-file-into.html
'''
import
fileinput
import
json
import
csv
import
sys
EPILOG
=
'''Usage: %(prog)s -i [input JSON file] > [output TSV file]'''
def
main
():
import
argparse
parser
=
argparse
.
ArgumentParser
(
description
=
__doc__
,
epilog
=
EPILOG
,
formatter_class
=
argparse
.
RawDescriptionHelpFormatter
,
)
parser
.
add_argument
(
'--infile'
,
'-i'
,
help
=
"JSON file to convert to TSV"
)
args
=
parser
.
parse_args
()
lines
=
[]
if
args
.
infile
:
with
open
(
args
.
infile
,
'r'
)
as
f
:
for
line
in
f
:
lines
.
append
(
line
)
else
:
print
>>
sys
.
stderr
,
"Usage: JSON2TSV.py -i [input JSON file] > [output TSV file]"
sys
.
exit
(
0
)
new_json
=
json
.
loads
(
''
.
join
(
lines
))
keys
=
{}
for
i
in
new_json
:
for
k
in
i
.
keys
():
keys
[
k
]
=
1
tab_out
=
csv
.
DictWriter
(
sys
.
stdout
,
fieldnames
=
keys
.
keys
(),
dialect
=
'excel-tab'
)
tab_out
.
writeheader
()
for
row
in
new_json
:
tab_out
.
writerow
(
row
)
if
__name__
==
'__main__'
:
main
()
Back
|
FazBrowse Home
|
New Git URL