Page Menu
Home
DevCentral
Search
Configure Global Search
Log In
Files
F24928972
spreadsheet2json
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
979 B
Referenced Files
None
Subscribers
None
spreadsheet2json
View Options
#!/usr/bin/env python3
#
# Converts key/value lines from a spreadsheet to a JSON object:
# 1. create a spreadsheet with two columns, one for keys, one for values
# 2. copy paste into a text file (or save as a file delimited with tabs)
# 3. `spreadsheet2json neko.dat > neko.json`
import
collections
import
json
import
sys
import
os.path
# Parses arguments
if
len
(
sys
.
argv
)
<
2
:
print
(
"Usage: spreadsheet2json <some-copy-paste-of-a-spreadsheet.dat>"
)
sys
.
exit
(
1
)
source
=
sys
.
argv
[
1
]
if
not
os
.
path
.
exists
(
source
):
print
(
"File not found:"
,
source
)
sys
.
exit
(
2
)
# Reads tabular data file and produce JSON
with
open
(
source
,
"r"
)
as
translations_file
:
translations
=
{}
for
line
in
translations_file
:
record
=
line
.
rstrip
()
.
split
(
'
\t
'
)
translations
[
record
[
0
]]
=
record
[
1
]
# Sorted JSON output
sorted_translations
=
collections
.
OrderedDict
(
sorted
(
translations
.
items
()))
json
=
json
.
dumps
(
sorted_translations
,
indent
=
4
)
print
(
json
)
File Metadata
Details
Attached
Mime Type
text/x-python
Expires
Sat, Mar 21, 07:05 (1 d, 16 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
3546606
Default Alt Text
spreadsheet2json (979 B)
Attached To
Mode
rDWD Dæghrefn Wikidata access layer
Attached
Detach File
Event Timeline
Log In to Comment