Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: no newlines in base lists #239

Open
jessexknight opened this issue Oct 10, 2018 · 2 comments
Open

Feature Request: no newlines in base lists #239

jessexknight opened this issue Oct 10, 2018 · 2 comments

Comments

@jessexknight
Copy link

I have some content with long and sometimes deeply nested lists (from np.ndarray.tolist()). While it is readable to print with newlines for every element, it would IMO be much cleaner to omit newlines in the inner-most list, and replace with spaces. For example,

Currently:

{
 "arr": [
  [
   0.0,
   null,
   0.468
  ],
  [
   0.0,
   null,
   0.688
  ],
  [
   0.0,
   0.32,
   null
  ],
  [
   0.0,
   0.25,
   null
  ]
 ]
 }
}

Desired:

{
 "arr": [
  [ 0.0, null ,0.468 ],
  [ 0.0, null, 0.688 ],
  [ 0.0, 0.32, null ],
  [ 0.0, 0.25, null ]
 ]
}

Would this be considered? Or too much of a special case?
Thanks,

@etrepum
Copy link
Member

etrepum commented Oct 10, 2018

It’s an interesting use case, but pretty printing is a bit complicated in the general case. If you had a PR to implement this it would be strongly considered so long as it doesn’t affect performance of the normal case.

@jessexknight
Copy link
Author

I'll see what I can do when I get some free time!

FYI, for now, my workaround looks like this (I also only want to "flatten" numeric arrays):

import re,simplejson
# ...
def jsondump(obj,fp,**kwargs):
  s = simplejson.dumps(obj,**kwargs)
  for match in re.finditer('\[((?ms)\n|\s|\d|\.|\,|null)*\]',s):
    s = s.replace(match.group(0),
                  match.group(0).replace(' ','').replace('\n',' '))
  fp.write(s)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants