pprint

How do I get python's pprint to return a string instead of printing?

元气小坏坏 提交于 2021-01-16 04:46:26
问题 In other words, what's the sprintf equivalent to pprint? 回答1: The pprint module has a command named pformat, for just that purpose. From the documentation: Return the formatted representation of object as a string. indent, width and depth will be passed to the PrettyPrinter constructor as formatting parameters. Example: >>> import pprint >>> people = [ ... {"first": "Brian", "last": "Kernighan"}, ... {"first": "Dennis", "last": "Richie"}, ... ] >>> pprint.pformat(people, indent=4) "[ { 'first

How do I get python's pprint to return a string instead of printing?

为君一笑 提交于 2021-01-16 04:44:50
问题 In other words, what's the sprintf equivalent to pprint? 回答1: The pprint module has a command named pformat, for just that purpose. From the documentation: Return the formatted representation of object as a string. indent, width and depth will be passed to the PrettyPrinter constructor as formatting parameters. Example: >>> import pprint >>> people = [ ... {"first": "Brian", "last": "Kernighan"}, ... {"first": "Dennis", "last": "Richie"}, ... ] >>> pprint.pformat(people, indent=4) "[ { 'first

Can I make pprint in python3 not split strings, like in python2?

混江龙づ霸主 提交于 2020-01-01 08:36:14
问题 Is there a way to tell pprint in Python 3 not to split strings on whitespace? Python 2's pprint did not do this. Can this behavior be disabled? I looked through the source for pprint and it doesn't look like there's an option I saw for this. Can I trick it somehow? Here's an example of what I'm getting: >>> PP.pprint("ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ ZZZZZ",width=-1,compact=True) (

pprint dictionary on multiple lines

自闭症网瘾萝莉.ら 提交于 2019-12-28 11:42:33
问题 I'm trying to get a pretty print of a dictionary, but I'm having no luck: >>> import pprint >>> a = {'first': 123, 'second': 456, 'third': {1:1, 2:2}} >>> pprint.pprint(a) {'first': 123, 'second': 456, 'third': {1: 1, 2: 2}} I wanted the output to be on multiple lines, something like this: {'first': 123, 'second': 456, 'third': {1: 1, 2: 2} } Can pprint do this? If not, then which module does it? I'm using Python 2.7.3. 回答1: Use width=1 or width=-1 : In [33]: pprint.pprint(a, width=1) {'first

pprint with custom float formats

前提是你 提交于 2019-12-23 19:29:49
问题 I have a nested dictionary structure with tuple keys. Here's what an entry looks like when I pretty-print the dictionary using pprint: ... ('A', 'B'): {'C': 0.14285714285714285, 'D': 0.14285714285714285, 'E': 0.14285714285714285, 'F': 0.14285714285714285, 'G': 0.14285714285714285, 'H': 0.14285714285714285, 'I': 0.14285714285714285}, ... It's pretty nifty, but I'd like to customize it further by cutting down some extra digits from the floats. I was thinking that it'd be possible to achieve by

Parsing json and searching through it

天涯浪子 提交于 2019-12-17 15:42:27
问题 I have this code import json from pprint import pprint json_data=open('bookmarks.json') jdata = json.load(json_data) pprint (jdata) json_data.close() How can I search through it for u'uri': u'http: ? 回答1: As json.loads simply returns a dict, you can use the operators that apply to dicts: >>> jdata = json.load('{"uri": "http:", "foo", "bar"}') >>> 'uri' in jdata # Check if 'uri' is in jdata's keys True >>> jdata['uri'] # Will return the value belonging to the key 'uri' u'http:' Edit: to give

How to remove timestamps from celery pprint output?

ⅰ亾dé卋堺 提交于 2019-12-12 11:14:10
问题 When running the celery worker then each line of the output of the pprint is always prefixed by the timestamp and also is being stripped. This makes it quite unreadable: [2015-11-05 16:01:12,122: WARNING/Worker-2] { [2015-11-05 16:01:12,122: WARNING/Worker-2] u'key1' [2015-11-05 16:01:12,122: WARNING/Worker-2] : [2015-11-05 16:01:12,122: WARNING/Worker-2] 'value1' [2015-11-05 16:01:12,122: WARNING/Worker-2] , u'_id': [2015-11-05 16:01:12,122: WARNING/Worker-2] ObjectId(

How to use pprint to print an object using the built-in __str__(self) method?

江枫思渺然 提交于 2019-12-12 07:48:41
问题 I have a Python script which processes a .txt file which contains report usage information. I'd like to find a way to cleanly print the attributes of an object using pprint's pprint(vars(object)) function. The script reads the file and creates instances of a Report class. Here's the class. class Report(object): def __init__(self, line, headers): self.date_added=get_column_by_header(line,headers,"Date Added") self.user=get_column_by_header(line,headers,"Login ID") self.report=get_column_by