Python 3-将字符串转换为字节的问题

Python 3-将字符串转换为字节的问题,python,xml,byte,crc,crc32,Python,Xml,Byte,Crc,Crc32,我正在学习Python 3,对此我找不到任何解释。 以下函数从XML字符串中提取并返回内容: import xml.etree.ElementTree as ET import zlib def parse_xml(session_id): root = ET.ElementTree(ET.fromstring(session_id)) session_xml = root.findall( './/{http://www.api-url.com/API_Ser

我正在学习Python 3,对此我找不到任何解释。 以下函数从XML字符串中提取并返回内容:

import xml.etree.ElementTree as ET
import zlib

def parse_xml(session_id):
    root = ET.ElementTree(ET.fromstring(session_id))
    session_xml = root.findall(
        './/{http://www.api-url.com/API_Service/}StartSessionResult')[0].text
    return session_xml
会话id的示例值

<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><StartSessionResponse xmlns="http://www.elektrotop3001.com/AAAA_Service/"><StartSessionResult>&lt;?xml version="1.0" encoding="utf-8"?&gt;&lt;AUTHENTICATION&gt;&lt;SESSSION ID="3btqqSR5AUQSnDVBQ3dd5LAj54rekna3HPox7vYe" /&gt;&lt;/AUTHENTICATION&gt;</StartSessionResult></StartSessionResponse></soap:Body></soap:Envelope>
稍后,我将使用
.encode('utf-8')
会话xml
字符串转换为字节:

问题是它将
\xef\xbb\xbf
添加到bytes对象。 当我手动将示例普通字符串放入
return
类似
return'abc'
时,
sess\u short\u xml\u bytes
的值是
b'abc'
,然后它不添加
\xef\xbb\xbf
。 这是一个问题,因为稍后我将使用
zlib.crc32
(需要类似字节的对象)从
sess\u short\u xml\u字节计算CRC,CRC结果可能无效,因为
\xef\xbb\xbf

是否有任何方法可以在不修改XML内容的情况下将其提取并转换为字节?或者有什么方法可以从XML内容中正确计算JAMCRC?

这些字节是Unicode字符U+FEFF的UTF-8编码,零宽度不间断空格,通常用作字节顺序标记。你确定里面没有这种东西吗?他们可能很难看到……就是这样!:-)非常感谢你。为了解决这个问题,我做了。在将sess_short_xml转换为字节之前替换(u'\ufeff','')。
<?xml version="1.0" encoding="utf-8"?><AUTHENTICATION><SESSSION ID="3btqqSR5AUQSnDVBQ3dd5LAj54rekna3HPox7vYe" /></AUTHENTICATION>
sess_short_xml = parse_xml(session_id)
sess_short_xml_bytes = sess_short_xml.encode('utf-8')