【问题标题】:Getting syntax error while running the code in a particular line in pandas在熊猫的特定行中运行代码时出现语法错误
【发布时间】:2021-06-15 15:08:09
【问题描述】:

这行显示错误:

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

【问题讨论】:

  • 双引号字符串中有未转义的双引号字符。对外部字符串使用单引号,或者转义内部双引号。

标签: python pandas jupyter-notebook syntax-error


【解决方案1】:

试试这个:

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"

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

【讨论】:

  • 这会改变文本本身的值,这会带来问题。也许他们应该在外面使用单引号。
  • Shir,它起作用了,但是如何确定我们应该在哪里使用单引号或双引号?
  • 我编辑了解决方案以提供更多信息。希望这能回答您的问题。
  • Shir,但是str1和str2的O/P是一样的,有什么区别?
  • 我不确定我是否理解您的问题,但this post 似乎在讨论类似的问题。如果您还有其他问题,请告诉我。
猜你喜欢
  • 2018-01-24
  • 1970-01-01
  • 1970-01-01
  • 2016-04-15
  • 2018-01-08
  • 1970-01-01
  • 2019-09-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多