【问题标题】:TypeError: an integer is required (got type str) on function join - pythonTypeError:函数连接时需要一个整数(获取类型 str)-python
【发布时间】:2016-01-25 09:23:54
【问题描述】:

我想将照片数据存储在 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

但我得到了错误

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

有人可以帮我解释一下这是怎么回事吗? 提前致谢。

【问题讨论】:

  • 错误告诉你,你正在通过使用 map() 将字符串传递给函数 chr()。我不知道 self.data_list 是什么,但它看起来像一个字符串列表。
  • 你认为chr 是做什么的?你为什么叫它?

标签: python string image join integer


【解决方案1】:

我怀疑您的问题实际上来自在读取和写入文件时未使用 binary 模式。请参阅this question 了解在 Python 中对二进制文件的基本读/写。

【讨论】:

    【解决方案2】:

    您的代码有点难以理解,但我很确定您只是想删除一个子字符串。您可以将 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"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-14
      • 1970-01-01
      • 1970-01-01
      • 2018-04-24
      • 1970-01-01
      • 2020-02-16
      相关资源
      最近更新 更多