我想在python文件中存储foto数据。但是我的文件中有一些奇怪的字符,所以这个文件不能正常打开。 我要做的是在将数据保存到文件中之前将其从数组中删除:
def save_foto(self):
""" Save foto data to a file """
self.data_aux = ''
last = 0
self.data_list = list(self.vFOTO)
for i in range(0,len(self.vFOTO)):
if(self.vFOTO[i]=='\x90' and self.vFOTO[i+1]=='\x00' and self.vFOTO[i+2]=='\x00' and self.vFOTO[i+3]=='\x00'
and self.vFOTO[i+4]=='\x00' and self.vFOTO[i+5]=='\x00' and self.vFOTO[i+6]=='\x00' and self.vFOTO[i+7]=='\x00'
and self.vFOTO[i+8]=='\x00' and self.vFOTO[i+9]=='\x00'):
aux1=''.join(map(chr,self.data_list[last:i]))
self.data_aux = self.data_aux+aux1
i=i+10
last=i
但我得到了错误
"TypeError: an integer is required (got type str)" on line aux1=''.join(map(chr,self.data_list[last:i])).
有人能帮我解释一下发生了什么事吗? 提前谢谢。在
我怀疑你的问题实际上是因为读写文件时没有使用二进制模式。有关Python中二进制文件的基本读/写,请参见this question。在
你的代码有点难以理解,但我很确定你只是想删除一个子字符串。你可以利用结构更换()使用unicode字符串:
.replace(old,new,max)其中old是要查找的子字符串,new是要替换它的内容,max是要限制的匹配数(默认为子字符串的所有实例)。在
^{pr2}$相关问题 更多 >
编程相关推荐