2

I want to store foto data in file in python. But I get some strange characters in my file so this files does not open properly. What I'm trying to do is remove this data from my array before save it in a file:

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

but I get the error

"TypeError: an integer is required (got type str)" on line aux1=''.join(map(chr,self.data_list[last:i])).

Can some one help me and explain me whats goin on? Thanks in advance.

Paul Rooney
  • 20,879
  • 9
  • 40
  • 61
JNeto06
  • 25
  • 5
  • 3
    The error is telling you that you're passing a string to the function chr() through your use of map(). I don't know what self.data_list is, but it looks like a list of strings. – Neil Jan 25 '16 at 00:57
  • What do you think `chr` does? Why are you calling it? – user2357112 Jan 25 '16 at 00:58

2 Answers2

1

I suspect your problem actually comes from not using binary mode when reading and writing your file. See this question for basic read/write to a binary file in Python.

Community
  • 1
  • 1
aghast
  • 14,785
  • 3
  • 24
  • 56
-1

Your code is a bit tough to follow, but I am pretty sure you're just trying to remove a substring. You could use str.replace() with a unicode string:

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

.replace(old, new, max) where old is the substring to find, new is what to replace it with and max is the number of matches to limit to (default is all instances of the substring).

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"