gpt4 book ai didi

python - Openpyxl Unicode 值

转载 作者:太空宇宙 更新时间:2023-11-03 11:50:06 24 4
gpt4 key购买 nike

我正在使用 openpyxl 从 Excel 电子表格中读取单元格值。其中一个单元格的值由换行符分隔。我想使用换行符作为分隔符来拆分字符串。然而,openpyxl 似乎将回车序列化为非标准格式。请看下面的示例。

代码

import openpyxl

# Open the worksheet
wb = openpyxl.load_workbook(wb_path)
ws = wb.get_sheet_by_name("testing")

# Get the string value
tests_str = ws.cell(row = row, column = column).value

# Split text on newlines and add them to the list
tests = []
for test in tests_str.splitlines():
tests.append(test)

输出

>>> tests_str
u'Test1_x000D_\nTest2_x000D_\nTest3_x000D_'
>>> tests
[u'Test1_x000D_', u'Test2_x000D_', u'Test3_x000D_']

openpyxl 似乎将 \r 字符序列化为 _x000D_ 这就是为什么 splitlines() 不是将其作为换行符删除。 openpyxl 的行为是这样的吗?我做错了什么吗?

最佳答案

some support issue from 2015 中所述( see Google cache entry to avoid login ),在openpyxl的官方Bitbucket项目中已经发布,这是用Excel完成的,似乎不受openpyxl的控制。

为了解决这个问题,有一些用于编码/解码的实用函数

>> openpyxl.utils.escape.unescape(tests_str))
u'Test1\r\nTest2\r\nTest3\r'

文档链接:https://openpyxl.readthedocs.io/en/stable/api/openpyxl.utils.escape.html

关于python - Openpyxl Unicode 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29976234/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com