运行 pandas 中特定行的代码出现语法错误

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

这行显示错误:

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

错误:

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

试试这个:

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

更一般地说,您可以使用单引号或双引号在 python 中定义字符串:

str1 = "hi there"
str2 = 'goodbye'

但是,如果你想定义一个内部带有单引号/双引号的字符串,你需要在外部使用另一种来定义字符串:

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"

您需要交替使用单引号和双引号以使字符串保持一致。