UnicodeEncodeError:' ascii'编解码器不能对字符u' \ u0446'进行编码。位置32:序数不在范围内(128)

时间:2016-08-08 20:50:00

标签: python unicode

我试图调试前一个实习生所写的一些代码,而且我在解决这个问题时遇到了一些困难,其中包含来自其他unicode错误帖子的答案。

错误发现在此函数的最后一行:

    def dumpTextPacket(self, header, bugLog, offset, outfile):
        bugLog.seek(offset)
        data = bugLog.read( header[1] )        # header[1] = size of the packet 
        outString = data.decode("utf-8","ignore")
        if(header[3] == 8): # Removing ugly characters from packet that has bTag = 8.
            outString = outString[1:]
            outString = outString.strip('\0')  # Remove all 'null' characters from text
        outString = "{:.3f}".format(header[5]) + ' ms: ' + outString              # Append the timestamp to the beginning of the line
        outfile.write(outString)

我对unicode没有多少经验,所以我非常感谢有关此问题的任何指示!

编辑:使用Python 2.7,以下是整个文件。我应该提到的另一件事是代码在解析某些文件时确实有效,但是当时间戳太大时我觉得它在其他文件上有错误吗?

在main.py文件中,我们调用LogInterpreter.execute()方法,并且traceback在行的标题上显示错误" outfile.write(outString)",最后一个dumpTextPacket方法中的行,在execute方法中调用:

import sys
import os
from struct import unpack
class LogInterpreter:

def __init__( self ):
    self.RTCUpdated = False
    self.RTCOffset = 0.0
    self.LastTimeStamp = 0.0
    self.TimerRolloverCount = 0
    self.ThisTimeStamp = 0.0

    self.m_RTCSeconds = 0.0
    self.m_StartTimeInSec = 0.0

def GetRTCOffset( self ):
    return self.m_RTCSeconds - self.m_StartTimeInSec

def convertTimeStamp(self,uTime,LogRev):
    TicsPerSecond = 24000000.0

    self.ThisTimeStamp = uTime
    self.RTCOffset = self.GetRTCOffset()

    if int( LogRev ) == 2:
        if self.RTCUpdated:
            self.LastTimeStamp = 0.0
        if self.LastTimeStamp > self.ThisTimeStamp:
            self.TimerRolloverCount += 1
        self.LastTimeStamp = self.ThisTimeStamp

    ULnumber = (-1 & 0xffffffff)

    return ((ULnumber/TicsPerSecond)*self.TimerRolloverCount + (uTime/TicsPerSecond) + self.RTCOffset) * 1000.0

##########################################################################
# Information about the header for the current packet we are looking at. #                                   
##########################################################################
def grabHeader(self, bugLog, offset):
    '''
    s_PktHdrRev1
    /*0*/    u16 StartOfPacketMarker; # uShort 2   
    /*2*/    u16 SizeOfPacket;        # uShort 2   
    /*4*/    u08 LogRev;              # uChar  1    
    /*5*/    u08 bTag;                # uChar  1    
    /*6*/    u16 iSeq;                # uShort 2   
    /*8*/    u32 uTime;               # uLong  4
    '''
    headerSize = 12 # Header size in bytes
    bType = 'HHBBHL' # codes for our byte type
    bugLog.seek(offset)
    data = bugLog.read(headerSize)

    if len(data) < headerSize:
        print('Error in the format of BBLog file')
        sys.exit()

    headerArray = unpack(bType, data)
    convertedTime = self.convertTimeStamp(headerArray[5],headerArray[2])
    headerArray = headerArray[:5] + (convertedTime,)
    return headerArray

################################################################
# bTag = 8 or bTag = 16 --> just write the data to LogMsgs.txt #
################################################################
def dumpTextPacket(self, header, bugLog, offset, outfile):
    bugLog.seek(offset)
    data = bugLog.read( header[1] )                               # header[1] = size of the packet 
    outString = data.decode("utf-8","ignore")
    if(header[3] == 8):                                           # Removing ugly characters from packet that has bTag = 8.
        outString = outString[1:]
        outString = outString.strip('\0')                         # Remove all 'null' characters from text
    outString = "{:.3f}".format(header[5]) + ' ms: ' + outString  # Append the timestamp to the beginning of the line
    outfile.write(outString)



def execute(self):
    path = './Logs/'
    for fn in os.listdir(path):
        fileName = fn
        print fn
        if (fileName.endswith(".bin")): 
        # if(fileName.split('.')[1] == "bin"):
            print("Parsing "+fileName)
            outfile = open(path+fileName.split('.')[0]+".txt", "w")           # Open a file for output
            fileSize = os.path.getsize(path+fileName)
            packetOffset = 0
            with open(path+fileName, 'rb') as bugLog:
                while(packetOffset < fileSize):
                    currHeader = self.grabHeader(bugLog, packetOffset)        # Grab the header for the current packet
                    packetOffset = packetOffset + 12                          # Increment the pointer by 12 bytes (size of a header packet)
                    if currHeader[3]==8 or currHeader[3]==16:                 # Look at the bTag and see if it is a text packet
                        self.dumpTextPacket(currHeader, bugLog, packetOffset, outfile)
                    packetOffset = packetOffset + currHeader[1]               # Move on to the next packet by incrementing the pointer by the size of the current packet
            outfile.close()
            print(fileName+" completed.")

1 个答案:

答案 0 :(得分:0)

当你将两个字符串中的一个添加为Unicode时,Python 2也会将结果强制转换为Unicode。

>>> 'a' + u'b'
u'ab'

由于您使用了data.decodeoutString将是Unicode。

写入二进制文件时,必须有一个字节字符串。 Python 2将尝试将您的Unicode字符串转换为字节字符串,但它使用的是最通用的编解码器:'ascii'。此编解码器在许多Unicode字符上失败,特别是那些代码点高于'\u007f'的字符。您可以使用功能更强大的编解码器自行编码,以解决此问题:

outfile.write(outString.encode('utf-8'))

Python 3中的所有内容都发生了变化,它不会让您混合字节字符串和Unicode字符串,也不会尝试任何自动转换。