【发布时间】: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 3.7 OpenPyxl 在我的 Excel 文件中,单元格值为“HP 934XL/935XL” 我的代码用一个简单的指令读取值: cle = sheet.cell(row=i,column=1).value
调试或打印时的值为
'HP 934XL\/935XL'
这是这个 OpenPyxl 的标准行为吗?
【问题讨论】:
/ 已被转义,因此变为\/。
使用: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!
【讨论】: