2

I am trying to generate a random string using loremipsum package

from loremipsum import generate_paragraph, get_sentences, get_sentence
sentences_count, words_count, paragraph = generate_paragraph()
print(paragraph)

It outputs a string with byte prefixes i.e., each word is pre-fixed with a with b or B.

B'ante' b'ut' b'a' b'ut' b'a'. B'nibh' b'ad' b'in' b'non' b'a' b'dui' b'nunc' b'a'. B'diam' b'at' b'a' b'eros' b'a'. B'quis' b'at' b'a' b'a' b'a' b'a' b'eros' b'a' b'porta'. B'orci' b'id' b'mi' b'ut' b'eleifend' b'fames' b'rutrum' b'at' b'luctus' b'diam'. B'arcu' b'et'. B'pede' b'ut' b'a' b'a'. B'elit' b'at' b'pretium' b'netus' b'amet' b'nunc'. B'nunc' b'eu' b'a' b'mi' b'ultricies' b'ut' b'a' b'etiam' b'quis'. B'nisl' b'mi'. B'orci' b'id' b'a'. B'eget' b'ad' b'a' b'a' b'a' b'ad' b'magna' b'pretium' b'sed'. B'elit' b'eu' b'a' b'congue' b've' b'a'

How can I get rid of these byte prefixes in python 3.6.5.

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Shani
  • 447
  • 5
  • 18
  • Possible duplicate of [How do I get rid of the b-prefix in a string in python?](https://stackoverflow.com/questions/41918836/how-do-i-get-rid-of-the-b-prefix-in-a-string-in-python) – Shubham May 02 '18 at 11:35
  • 1
    This is not a duplicate, but an error in the `loremipsum` libary. – MegaIng May 02 '18 at 11:44
  • Indeed completely broken. I am using `python-lorem` from [here](https://github.com/JarryShaw/lorem) – Raffi Dec 14 '20 at 09:52

2 Answers2

3

The loremipsum is broken, because they return a string containing formatted bytes, which makes zero sense. To temporarily fix this, you can use this function:

import re
from loremipsum import generate_paragraph

def fix_loremipsum(loremipsum_string):
    loremipsum_string = re.sub("B'(.*?)'", lambda x: x.group(1).title(), loremipsum_string)
    loremipsum_string = re.sub("b'(.*?)'", lambda x: x.group(1), loremipsum_string)
    return loremipsum_string


sentences_count, words_count, paragraph = generate_paragraph()
paragraph = fix_loremipsum(paragraph)
print(paragraph)
MegaIng
  • 7,361
  • 1
  • 22
  • 35
  • I have tried this earlier, and getting the following error: AttributeError: 'str' object has no attribute 'decode'. I believe this is specific to python 3.x – Shani May 02 '18 at 11:42
  • 1
    I just tried, and found out that the `loremipsum` libary returns a string of formatted bytes. WTF. You should send the coder an email. – MegaIng May 02 '18 at 11:45
0

This works for me:

import loremipsum as l
l.generator._DICTIONARY = [i.decode('utf8') for i in l.generator._DICTIONARY]
l._GENERATOR = l.Generator()
print(l.get_paragraph())
Nick Jensen
  • 411
  • 4
  • 5