-1

I have a function which returns a list like below:

['Title 1', 'Title 2', 'Title 3', 'Title 4']

I have tried numerous ways of implementing the soup.prettify() function to pretty print my list. However can't figure out the exact arrangement to make this work.

Below is my function:

def get_titles(html):
    soup = BeautifulSoup(html,'html.parser')
    outcome = soup.div.find_all(text=True)
    return(outcome)

Calling the function:

html = '#the example html'
print(get_titles(html))

Again, the current outcome (not exact, just an example of the list):

['Title 1', 'Title 2', 'Title 3', 'Title 4']

For clarity, i'm looking to utilise .prettify() to make the outcome list easier to read and therefore 'prettier'.

howard
  • 95
  • 6
  • something like print('\n'.join(get_titles(html)))? – Sergey K Apr 29 '22 at 14:03
  • you pretiffy a `bs4.Tag` (like your `soup`) object not a list – cards Apr 29 '22 at 14:04
  • I tried to print using the following " print(soup.prettify(get_event_titles(html)) " but receive the error "SyntaxError: unexpected EOF while parsing" – howard Apr 29 '22 at 14:18
  • 1
    `pretiffy` needs `bs4 object`, not `list`. With `list` you have to use standard function to format data - like `"\n".join(list)` or module `pprint` (PrettyPrint) and `pprint.pprint(list)` – furas Apr 29 '22 at 14:40
  • 12 years old question: [How to print a list in Python "nicely" - Stack Overflow](https://stackoverflow.com/questions/1523660/how-to-print-a-list-in-python-nicely) – furas Apr 29 '22 at 14:59
  • Please add your desired output format to your question – Martin Evans May 02 '22 at 16:53

1 Answers1

2

.pretiffy() needs bs4 object, not list.

Standard method to make list more readable is to convert to string using .join() with \n between elements.

print("\n".join(['Title 1', 'Title 2', 'Title 3', 'Title 4']))

Result:

Title 1
Title 2
Title 3
Title 4

There is also standard module pprint (PrettyPrint) which can format some data

import pprint

pprint.pprint(['Title 1', 'Title 2', 'Title 3', 'Title 4'], width=20)

Result:

['Title 1',
 'Title 2',
 'Title 3',
 'Title 4']

You may also use standard module json to format data
(but this works only with standard data types - like list, dict, but not class)

import json

print(json.dumps(['Title 1', 'Title 2', 'Title 3', 'Title 4'], indent=2))

Result:

[
  "Title 1",
  "Title 2",
  "Title 3",
  "Title 4"
]

EDIT:

Of course you can also write own function to display list
(but this will not prettify nested elements/lists/dictionares)

def show_list(data):
    print('[')
    print('  ' + ",\n  ".join(f'"{x}"' for x in data) + ',')
    print(']')
   

data = ['Title 1', 'Title 2', 'Title 3', 'Title 4']
show_list(data)

Result:

[
  "Title 1",
  "Title 2",
  "Title 3",
  "Title 4",
]

EDIT:

There is also nice module Rich which also has function pprint() and it can use colors to display different type of data

from rich.pretty import pprint

data = ['Title 1', 'Title 2', 'Title 3', 'Title 4', 
        1234, {'A':'Hello', 'B':'World', 'C':'Python'}]

pprint(data, expand_all=True)    

Result:

enter image description here

Image from page Rich:

enter image description here

furas
  • 134,197
  • 12
  • 106
  • 148