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.

3
  • Possible duplicate of How do I get rid of the b-prefix in a string in python? Commented May 2, 2018 at 11:35
  • 1
    This is not a duplicate, but an error in the loremipsum libary. Commented May 2, 2018 at 11:44
  • Indeed completely broken. I am using python-lorem from here Commented Dec 14, 2020 at 9:52

2 Answers 2

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)
Sign up to request clarification or add additional context in comments.

2 Comments

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
I just tried, and found out that the loremipsum libary returns a string of formatted bytes. WTF. You should send the coder an email.
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())

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.