ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

linux – 修复AttributeError:’file’对象没有属性’buffer'(Python3)

2019-07-10 18:53:22  阅读:118  来源: 互联网

标签:linux type-conversion python-2-7 python-3-x


Ubuntu上的Python 2.7.我试过为Python3运行小python脚本(文件转换器),得到错误:

$python uboot_mdb_to_image.py < input.txt > output.bin
Traceback (most recent call last):
  File "uboot_mdb_to_image.py", line 29, in <module>
    ascii_stdin = io.TextIOWrapper(sys.stdin.buffer, encoding='ascii', errors='strict')
AttributeError: 'file' object has no attribute 'buffer'

我怀疑它是由python 3和python 2之间的语法差异引起的,这里是脚本本身:

#!/usr/bin/env python3

import sys
import io

BYTES_IN_LINE = 0x10 # Number of bytes to expect in each line

c_addr = None
hex_to_ch = {}

ascii_stdin = io.TextIOWrapper(sys.stdin.buffer, encoding='ascii', errors='strict')

for line in ascii_stdin:
    line = line[:-1] # Strip the linefeed (we can't strip all white
                     # space here, think of a line of 0x20s)
    data, ascii_data = line.split("    ", maxsplit = 1)
    straddr, strdata = data.split(maxsplit = 1)
    addr = int.from_bytes(bytes.fromhex(straddr[:-1]), byteorder = 'big')
    if c_addr != addr - BYTES_IN_LINE:
        if c_addr:
            sys.exit("Unexpected c_addr in line: '%s'" % line)
    c_addr = addr
    data = bytes.fromhex(strdata)
    if len(data) != BYTES_IN_LINE:
        sys.exit("Unexpected number of bytes in line: '%s'" % line)
    # Verify that the mapping from hex data to ASCII is consistent (sanity check for transmission errors)
    for b, c in zip(data, ascii_data):
        try:
            if hex_to_ch[b] != c:
                sys.exit("Inconsistency between hex data and ASCII data in line (or the lines before): '%s'" % line)
        except KeyError:
            hex_to_ch[b] = c
    sys.stdout.buffer.write(data)

任何人都可以建议如何解决这个问题吗?

解决方法:

这是一个老问题,但是因为我遇到了类似的问题,所以当谷歌搜索错误时它首先出现了……

是的,它是由Python 3和2之间的差异引起的.在Python 3中,sys.stdin包含在io.TextIOWrapper中.在Python 2中,它是一个文件对象,它没有缓冲区属性. stderr和stdout也是如此.

在这种情况下,使用编解码器标准库可以实现Python 2中的相同功能:

ascii_stdin = codecs.getreader("ascii")(sys.stdin, errors="strict")

但是,此代码段提供了codecs.StreamReader的实例,而不是io.TextIOWrapper,因此可能不适用于其他情况.而且,遗憾的是,在io.TextIOWrapper中包装Python 2 stdin并非易事 – 请参阅Wrap an open stream with io.TextIOWrapper以获得更多讨论.

有问题的脚本有更多的Python 2不可兼容性.与相关问题相关,sys.stdout没有缓冲区属性,因此最后一行应该是

sys.stdout.write(data)

我能发现的其他事情:

> str.split没有maxsplit参数.请改用line.split(“”)[:2].
> int没有from_bytes属性.但int(straddr [: – 1] .encode(‘hex’),16)似乎是等价的.
> bytes类型仅限Python 3.在Python 2中,它是str的别名.

标签:linux,type-conversion,python-2-7,python-3-x
来源: https://codeday.me/bug/20190710/1426452.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有