1

I would like to assign the following expression to a variable:

textFormat = 'soup.find("div", {"class" : "article-entry text"}).text.replace(\'\n\', "")'

I am calling this code in another file using

text = exec(textFormat)

Sadly I get the error message:

   File "C:\Users\dadsad\documents\coding\dasdasd\functions.py", line 42, in loadAtom
    text = exec(textFormat)   File "<string>", line 1
    soup.find("div", {"class" : "article-entry text"}).text.replace('
                                                                    ^ SyntaxError: EOL while scanning string literal

Any ideas? Thanks! :)

Edit: Tried the suggestion, getting a None: enter image description here

4
  • 4
    It will need to be \\n Commented Nov 27, 2017 at 17:20
  • 3
    If you print(textFormat) and actually look at the value you assigned, you'll have a pretty good idea yourself. Commented Nov 27, 2017 at 17:20
  • 2
    The new line is interpreted and not passed to exec. That being said, exec and eval are antipatterns. Commented Nov 27, 2017 at 17:20
  • 1
    Yes, exec() always returns None. Perhaps you meant to use eval() instead? Commented Nov 27, 2017 at 18:08

1 Answer 1

5

I suspect you are suffering from backslashitis. You need one more slash before the n:

textFormat = 'soup.find("div", {"class" : "article-entry text"}).text.replace(\'\\n\', "")'

But, instead of execing code this way, you might want expose a subroutine instead:

def textFormat(soup):
    return soup.find("div", {"class" : "article-entry text"}).text.replace('\n', "")
Sign up to request clarification or add additional context in comments.

1 Comment

Tried your solution and added a screenshot. Still getting an error --> None

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.