0

I have the following while loop:

while workplace_tag.find_all_next('p')[l] != 'h1':
        abstract_tag = workplace_tag.find_all_next('p')[l]
        abstract = abstract_tag.text.strip().replace('\n', ' ').encode('windows-1252', errors='replace')
        l += 1
        print(abstract)

How do I join the <p> tags that are printed all into one line.

For example at the moment there might be something like this:

<p>1</p>
<p>2</p>
<p>3</p>
<p>4</p>

And I need it like this:

<p>1</p><p>2</p><p>3</p><p>4</p>

Thanks

grob
  • 111
  • 1
  • 8
  • 2
    `print(abstract, end = '')` – tgikal Oct 12 '18 at 13:19
  • 1
    The alternative would be to put them in a list. `abstracts = []` outside the loop, `abstracts.append(abstract)` instead of `print(abstract)` and lastly outside the loop (after) `print(''.join(abstracts))` – Anton vBR Oct 12 '18 at 13:21
  • 1
    @AntonvBR I was literally typing that. – tgikal Oct 12 '18 at 13:22
  • @tgikal hehe.your solution works too and is great... but it will make the next print statement be on the same row.. so maybe one print statement extra is needed in the end. Depends on the why are we printing in the first place though. – Anton vBR Oct 12 '18 at 13:23
  • @AntonvBR exactly the real method used here needs to be decided on what the end use is, if it is just to display than print would fine, if this information will be used somewhere else, than a list is much more useful. – tgikal Oct 12 '18 at 13:25
  • Possible duplicate of [How to print without newline or space?](https://stackoverflow.com/questions/493386/how-to-print-without-newline-or-space) – Ilhicas Oct 12 '18 at 13:45
  • This seems to break the loop for some reason, but the error i get is to do with my own code so not sure why it is breaking the loop – grob Oct 12 '18 at 13:49

2 Answers2

1

This will work (with the help of Keyur Potdar):

Create an empty string:

x = ''

Then concatenate each element in the loop:

x += abstract.decode('utf-8')

Output of x:

<p>1</p><p>2</p><p>3</p><p>4</p>
teller.py3
  • 822
  • 8
  • 22
  • 1
    The OP gets the text in a loop, and hence can't store it in the variables like you've shown. If you want to use this approach, you can do `x += abstract` inside the loop. – Keyur Potdar Oct 12 '18 at 13:54
  • I had this error? TypeError: can only concatenate str (not "bytes") to str – grob Oct 12 '18 at 14:12
  • See update on answer. Try again. The reason you are getting this error is because you are encoding it before concatenating (i have no idea why). So you have to decode it. – teller.py3 Oct 12 '18 at 14:16
  • It almost works, but it then infinitely prints the abstract and won't break the while loop? But it does put it all into one line – grob Oct 12 '18 at 14:19
  • This is because `workplace_tag.find_all_next('p')[l] != 'h1'` seems to be always true. We don't know the rest of your code. So we can't help you with that on this post. – teller.py3 Oct 12 '18 at 14:20
  • 1
    did it just had to put break in at the end, thanks for your help – grob Oct 12 '18 at 14:23
0

Replace

print(abstract)

with

print(abstract, end='')

if this is Python 3.

AKX
  • 152,115
  • 15
  • 115
  • 172
  • Why was this downvoted? This is the correct way, though it could also have the usage for python2. – Ilhicas Oct 12 '18 at 13:44