Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Openpyxl Unicode值_Python_Unicode_Openpyxl - Fatal编程技术网

Python Openpyxl Unicode值

Python Openpyxl Unicode值,python,unicode,openpyxl,Python,Unicode,Openpyxl,我正在使用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

我正在使用
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
字符序列化为
\u x000D\u
,这就是为什么
splitlines()
没有将其作为换行符删除的原因。
openpyxl
这样做有什么原因吗?我做错什么了吗?

看起来openpyxl或Excel正在以这种方式编码回车(
\r
,ASCII 0Dh)。您也可以将其转换回或拆分:

>>> s=u'Test1_x000D_\nTest2_x000D_\nTest3_x000D_'
>>> s.split('_x000D_\n')
[u'Test1', u'Test2', u'Test3_x000D_']     # This misses the final one.
>>> s.replace('_x000D_','').splitlines()  # Better...
[u'Test1', u'Test2', u'Test3']
正如在()中所述,它已经发布在openpyxl的官方Bitbucket项目中,这是由Excel完成的,对于openpyxl来说似乎失去了控制

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

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

文档链接:

Excel进行编码,openpyxl保留编码。