繁体   English   中英

TypeError:函数联接时需要一个整数(got type str)-python

[英]TypeError: an integer is required (got type str) on function join - python

我想将foto数据存储在python文件中。 但是我的文件中出现一些奇怪的字符,因此此文件无法正确打开。 我想做的是在将数据保存到文件之前,从数组中删除该数据:

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

但我得到了错误

aux1 =''。join(map(chr,self.data_list [last:i]))行上的“ TypeError:需要整数(类型为str)”。

有人可以帮我解释一下发生了什么吗? 提前致谢。

我怀疑您的问题实际上来自读写文件时未使用二进制模式。 有关在Python中对二进制文件的基本读/写操作,请参见此问题

您的代码很难遵循,但是我很确定您只是在尝试删除子字符串。 您可以将str.replace()与unicode字符串一起使用:

self.vFOTO.replace(u'\x90\x00\x00\x00\x00\x00\x00\x00\x00\x00', "")

.replace(old,new,max),其中old是要查找的子字符串,new是要替换的子字符串,max是要限制为的匹配数(默认为子字符串的所有实例)。

print "a a b c".replace("a", "d")
"d d b c"
print "a a b c".replace("a", "d", 1)
"d a b c"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM