以前のインターンが書いたコードをデバッグしようとしていますが、他のユニコードのエラー投稿からの回答でこの問題が発生しました。UnicodeEncodeError: 'ascii'コーデックは文字 'u0446'を位置32にエンコードできません:序数が範囲内にありません(128)
エラーは、この関数の最後の行にあります
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)
私はユニコードで多くの経験を持っていないので、私は、この問題を持つ任意のポインタを本当にいただければ幸いです!
edit:Python 2.7以降を使用するとファイル全体が表示されます。私が言及すべきもう一つのことは、いくつかのファイルを解析するときにコードが機能することですが、タイムスタンプが大きすぎると他のファイルでエラーが発生すると思いますか?
main.pyファイルでは、LogInterpreter.execute()メソッドを呼び出し、tracebackは、dumpTextPacketメソッドの最後の行である "outfile.write(outString)"行のタイトルにエラーを表示しますその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.")
また、関数に入力を追加できますか? –
あなたが書き込むファイルは、おそらくasciiコーデックを使用して開かれています。 Python 2または3を使用していますか? –
@DennisKuypers:もしこれがPython 2で、 'data'が既に' unicode'オブジェクトであれば 'decode'を実行しようとすると暗黙的に実際の' decodeの前にデフォルトのロケール設定(ASCIIを意味します) 'ステップ。しかし、確かにトレースバックを見る必要があります。 – ShadowRanger