Getting syntax error while running the code in a particular line in pandas

It shows me error in this line:

name = sel.xpath("//*[@class = "inline t-24 t-black t-normal break-words"]/text()").extract_first().split()

Error:

File "<ipython-input-9-f8d78586cc1a>", line 7
    name = sel.xpath("//*[@class = "inline t-24 t-black t-normal break-words"]/text()").extract_first().split()
                                         ^
SyntaxError: invalid syntax

Answer

Try this instead:

name = sel.xpath('//*[@class = "inline t-24 t-black t-normal break-words"]/text()1).extract_first().split()

More generally speaking, you may define strings in python using single quotes or double quotes:

str1 = "hi there"
str2 = 'goodbye'

However, if you with to define a string with single / double quotes inside, you need to use the other kind outside for string definition:

str3 = "he said: `I quote with single quotes`"
str4 = 'she said: "I quote with double quotes"'

print(str3) # yields he said: `I quote with single quotes`
print(str4) # yields she said: "I quote with double quotes"

You need to alternate single and double quotes for the string to remain consistent.

Leave a Reply

Your email address will not be published. Required fields are marked *