【问题标题】:OpenPyxl Getting the real cell value when cell's text contains a slashOpenPyxl当单元格的文本包含斜杠时获取真实的单元格值
【发布时间】:2021-04-30 12:50:53
【问题描述】:

Python 3.7 OpenPyxl 在我的 Excel 文件中,单元格值为“HP 934XL/935XL” 我的代码用一个简单的指令读取值: cle = sheet.cell(row=i,column=1).value

调试或打印时的值为

'HP 934XL\/935XL' 

这是这个 OpenPyxl 的标准行为吗?

【问题讨论】:

  • 转义不是来自openpyxl。

标签: python excel openpyxl


【解决方案1】:

/ 已被转义,因此变为\/

使用:
cle = sheet.cell(row=i,column=1).value.decode('string_escape')

.decode('unicode_escape')
取决于 Python 版本。

例子:

>>> print '"Hello,\\nworld!"'.decode('string_escape')
Hello,
world!

您还可以使用:

>>> import ast
>>> escaped_str = '"Hello,\\nworld!"'
>>> print ast.literal_eval(escaped_str)
Hello,
world!

【讨论】:

  • "/" 在 Python 中一般不会转义。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-31
相关资源
最近更新 更多