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.

2
  • 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. Commented Jan 25, 2016 at 0:57
  • What do you think chr does? Why are you calling it? Commented Jan 25, 2016 at 0:58

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

-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"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.