My python codes:
with open('outputFile.json', 'w') as outfile:
json.dump(ans, outfile, indent=4, separators=(',', ': '))
The output file is
[
{
"rowLength": 5,
"alphabet": [
"Q",
"W",
"I",
"B",
"P",
"A",
"S"
]
},
{
"rowLength": 3,
"alphabet": [
"S",
"D",
"E",
"U",
"I",
"O",
"L"
]
}
]
How to make the inner array into one line? Thanks
I think this could be error-prone if the format of the output ever changed but just as an idea?
>>> d = [{'rowLength': 5, 'alphabet': ['Q', 'W', 'I', 'B', 'P', 'A', 'S']}, {'rowLength': 3, 'alphabet': ['S', 'D', 'E', 'U', 'I', 'O', 'L']}]
>>> import json
>>> output = json.dumps(d, indent=4)
>>> import re
>>> print(re.sub(r'",\s+', '", ', output))
[
{
"rowLength": 5,
"alphabet": [
"Q", "W", "I", "B", "P", "A", "S"
]
},
{
"rowLength": 3,
"alphabet": [
"S", "D", "E", "U", "I", "O", "L"
]
}
]
Or with multiple replaces (something like this would be better):
>>> output = json.dumps(d, indent=4)
>>> output2 = re.sub(r'": \[\s+', '": [', output)
>>> output3 = re.sub(r'",\s+', '", ', output2)
>>> output4 = re.sub(r'"\s+\]', '"]', output3)
>>> print(output4)
[
{
"rowLength": 5,
"alphabet": ["Q", "W", "I", "B", "P", "A", "S"]
},
{
"rowLength": 3,
"alphabet": ["S", "D", "E", "U", "I", "O", "L"]
}
]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With