rStorms
rStorms

Reputation: 1115

Assign expression to a variable and execute it

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

Upvotes: 1

Views: 74

Answers (1)

Robᵩ
Robᵩ

Reputation: 168876

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', "")

Upvotes: 5

Related Questions