update format

pull/1050/head
Junkun 3 years ago
parent 3c8e87344a
commit 0cc81a52cd

@ -1,9 +1,7 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
# encoding: utf-8 # encoding: utf-8
# Copyright 2018 Nagoya University (Tomoki Hayashi) # Copyright 2018 Nagoya University (Tomoki Hayashi)
# Apache 2.0 (http://www.apache.org/licenses/LICENSE-2.0) # Apache 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
from __future__ import print_function from __future__ import print_function
from __future__ import unicode_literals from __future__ import unicode_literals
@ -12,7 +10,6 @@ import codecs
import json import json
import logging import logging
import sys import sys
from distutils.util import strtobool from distutils.util import strtobool
from espnet.utils.cli_utils import get_commandline_args from espnet.utils.cli_utils import get_commandline_args
@ -23,17 +20,16 @@ is_python2 = sys.version_info[0] == 2
def get_parser(): def get_parser():
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(
description="add multiple json values to an input or output value", description="add multiple json values to an input or output value",
formatter_class=argparse.ArgumentDefaultsHelpFormatter, formatter_class=argparse.ArgumentDefaultsHelpFormatter, )
)
parser.add_argument("jsons", type=str, nargs="+", help="json files") parser.add_argument("jsons", type=str, nargs="+", help="json files")
parser.add_argument( parser.add_argument(
"-i", "-i",
"--is-input", "--is-input",
default=True, default=True,
type=strtobool, type=strtobool,
help="If true, add to input. If false, add to output", help="If true, add to input. If false, add to output", )
) parser.add_argument(
parser.add_argument("--verbose", "-V", default=0, type=int, help="Verbose option") "--verbose", "-V", default=0, type=int, help="Verbose option")
return parser return parser
@ -121,7 +117,8 @@ if __name__ == "__main__":
out_add_dic = {} out_add_dic = {}
# add shape # add shape
if "odim" in adddic and "olen" in adddic: if "odim" in adddic and "olen" in adddic:
out_add_dic["shape"] = [int(adddic["olen"]), int(adddic["odim"])] out_add_dic[
"shape"] = [int(adddic["olen"]), int(adddic["odim"])]
elif "odim" in adddic: elif "odim" in adddic:
out_add_dic["shape"] = [int(adddic["odim"])] out_add_dic["shape"] = [int(adddic["odim"])]
# add all other key value # add all other key value
@ -143,13 +140,13 @@ if __name__ == "__main__":
# ensure "ensure_ascii=False", which is a bug # ensure "ensure_ascii=False", which is a bug
jsonstring = json.dumps( jsonstring = json.dumps(
{"utts": new_dic}, {
"utts": new_dic
},
indent=4, indent=4,
ensure_ascii=False, ensure_ascii=False,
sort_keys=True, sort_keys=True,
separators=(",", ": "), separators=(",", ": "), )
) sys.stdout = codecs.getwriter("utf-8")(sys.stdout
sys.stdout = codecs.getwriter("utf-8")( if is_python2 else sys.stdout.buffer)
sys.stdout if is_python2 else sys.stdout.buffer
)
print(jsonstring) print(jsonstring)

@ -1,6 +1,5 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
# encoding: utf-8 # encoding: utf-8
# Copyright 2017 Johns Hopkins University (Shinji Watanabe) # Copyright 2017 Johns Hopkins University (Shinji Watanabe)
# Apache 2.0 (http://www.apache.org/licenses/LICENSE-2.0) # Apache 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
from __future__ import print_function from __future__ import print_function
@ -17,8 +16,7 @@ is_python2 = sys.version_info[0] == 2
def get_parser(): def get_parser():
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(
description="convert scp to json", description="convert scp to json",
formatter_class=argparse.ArgumentDefaultsHelpFormatter, formatter_class=argparse.ArgumentDefaultsHelpFormatter, )
)
parser.add_argument("--key", "-k", type=str, help="key") parser.add_argument("--key", "-k", type=str, help="key")
return parser return parser
@ -28,10 +26,10 @@ if __name__ == "__main__":
args = parser.parse_args() args = parser.parse_args()
new_line = {} new_line = {}
sys.stdin = codecs.getreader("utf-8")(sys.stdin if is_python2 else sys.stdin.buffer) sys.stdin = codecs.getreader("utf-8")(sys.stdin
sys.stdout = codecs.getwriter("utf-8")( if is_python2 else sys.stdin.buffer)
sys.stdout if is_python2 else sys.stdout.buffer sys.stdout = codecs.getwriter("utf-8")(sys.stdout
) if is_python2 else sys.stdout.buffer)
line = sys.stdin.readline() line = sys.stdin.readline()
while line: while line:
x = line.rstrip().split() x = line.rstrip().split()
@ -43,6 +41,9 @@ if __name__ == "__main__":
# ensure "ensure_ascii=False", which is a bug # ensure "ensure_ascii=False", which is a bug
jsonstring = json.dumps( jsonstring = json.dumps(
all_l, indent=4, ensure_ascii=False, sort_keys=True, separators=(",", ": ") all_l,
) indent=4,
ensure_ascii=False,
sort_keys=True,
separators=(",", ": "))
print(jsonstring) print(jsonstring)

Loading…
Cancel
Save