当前位置: 首页>>代码示例>>Python>>正文


Python BytesIO.write方法代码示例

本文整理汇总了Python中io.BytesIO.write方法的典型用法代码示例。如果您正苦于以下问题:Python BytesIO.write方法的具体用法?Python BytesIO.write怎么用?Python BytesIO.write使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在io.BytesIO的用法示例。


在下文中一共展示了BytesIO.write方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: check_compressed

# 需要导入模块: from io import BytesIO [as 别名]
# 或者: from io.BytesIO import write [as 别名]
def check_compressed(self, fopen, suffixes):
        # Test that we can load data from a compressed file
        wanted = np.arange(6).reshape((2, 3))
        linesep = ('\n', '\r\n', '\r')
        for sep in linesep:
            data = '0 1 2' + sep + '3 4 5'
            for suffix in suffixes:
                with temppath(suffix=suffix) as name:
                    with fopen(name, mode='wt', encoding='UTF-32-LE') as f:
                        f.write(data)
                    res = self.loadfunc(name, encoding='UTF-32-LE')
                    assert_array_equal(res, wanted)
                    with fopen(name, "rt",  encoding='UTF-32-LE') as f:
                        res = self.loadfunc(f)
                    assert_array_equal(res, wanted)

    # Python2 .open does not support encoding 
开发者ID:Frank-qlu,项目名称:recruit,代码行数:19,代码来源:test_io.py

示例2: test_record

# 需要导入模块: from io import BytesIO [as 别名]
# 或者: from io.BytesIO import write [as 别名]
def test_record(self):
        c = TextIO()
        c.write('1 2\n3 4')
        c.seek(0)
        x = np.loadtxt(c, dtype=[('x', np.int32), ('y', np.int32)])
        a = np.array([(1, 2), (3, 4)], dtype=[('x', 'i4'), ('y', 'i4')])
        assert_array_equal(x, a)

        d = TextIO()
        d.write('M 64.0 75.0\nF 25.0 60.0')
        d.seek(0)
        mydescriptor = {'names': ('gender', 'age', 'weight'),
                        'formats': ('S1', 'i4', 'f4')}
        b = np.array([('M', 64.0, 75.0),
                      ('F', 25.0, 60.0)], dtype=mydescriptor)
        y = np.loadtxt(d, dtype=mydescriptor)
        assert_array_equal(y, b) 
开发者ID:Frank-qlu,项目名称:recruit,代码行数:19,代码来源:test_io.py

示例3: test_skiprows

# 需要导入模块: from io import BytesIO [as 别名]
# 或者: from io.BytesIO import write [as 别名]
def test_skiprows(self):
        c = TextIO()
        c.write('comment\n1,2,3,5\n')
        c.seek(0)
        x = np.loadtxt(c, dtype=int, delimiter=',',
                       skiprows=1)
        a = np.array([1, 2, 3, 5], int)
        assert_array_equal(x, a)

        c = TextIO()
        c.write('# comment\n1,2,3,5\n')
        c.seek(0)
        x = np.loadtxt(c, dtype=int, delimiter=',',
                       skiprows=1)
        a = np.array([1, 2, 3, 5], int)
        assert_array_equal(x, a) 
开发者ID:Frank-qlu,项目名称:recruit,代码行数:18,代码来源:test_io.py

示例4: test_complex_misformatted

# 需要导入模块: from io import BytesIO [as 别名]
# 或者: from io.BytesIO import write [as 别名]
def test_complex_misformatted(self):
        # test for backward compatibility
        # some complex formats used to generate x+-yj
        a = np.zeros((2, 2), dtype=np.complex128)
        re = np.pi
        im = np.e
        a[:] = re - 1.0j * im
        c = BytesIO()
        np.savetxt(c, a, fmt='%.16e')
        c.seek(0)
        txt = c.read()
        c.seek(0)
        # misformat the sign on the imaginary part, gh 7895
        txt_bad = txt.replace(b'e+00-', b'e00+-')
        assert_(txt_bad != txt)
        c.write(txt_bad)
        c.seek(0)
        res = np.loadtxt(c, dtype=complex)
        assert_equal(res, a) 
开发者ID:Frank-qlu,项目名称:recruit,代码行数:21,代码来源:test_io.py

示例5: test_max_rows_with_skiprows

# 需要导入模块: from io import BytesIO [as 别名]
# 或者: from io.BytesIO import write [as 别名]
def test_max_rows_with_skiprows(self):
        c = TextIO()
        c.write('comments\n1,2,3,5\n4,5,7,8\n2,1,4,5')
        c.seek(0)
        x = np.loadtxt(c, dtype=int, delimiter=',',
                       skiprows=1, max_rows=1)
        a = np.array([1, 2, 3, 5], int)
        assert_array_equal(x, a)

        c = TextIO()
        c.write('comment\n1,2,3,5\n4,5,7,8\n2,1,4,5')
        c.seek(0)
        x = np.loadtxt(c, dtype=int, delimiter=',',
                       skiprows=1, max_rows=2)
        a = np.array([[1, 2, 3, 5], [4, 5, 7, 8]], int)
        assert_array_equal(x, a) 
开发者ID:Frank-qlu,项目名称:recruit,代码行数:18,代码来源:test_io.py

示例6: test_utf8_file

# 需要导入模块: from io import BytesIO [as 别名]
# 或者: from io.BytesIO import write [as 别名]
def test_utf8_file(self):
        utf8 = b"\xcf\x96"
        with temppath() as path:
            with open(path, "wb") as f:
                f.write((b"test1,testNonethe" + utf8 + b",test3\n") * 2)
            test = np.genfromtxt(path, dtype=None, comments=None,
                                 delimiter=',', encoding="UTF-8")
            ctl = np.array([
                     ["test1", "testNonethe" + utf8.decode("UTF-8"), "test3"],
                     ["test1", "testNonethe" + utf8.decode("UTF-8"), "test3"]],
                     dtype=np.unicode)
            assert_array_equal(test, ctl)

            # test a mixed dtype
            with open(path, "wb") as f:
                f.write(b"0,testNonethe" + utf8)
            test = np.genfromtxt(path, dtype=None, comments=None,
                                 delimiter=',', encoding="UTF-8")
            assert_equal(test['f0'], 0)
            assert_equal(test['f1'], "testNonethe" + utf8.decode("UTF-8")) 
开发者ID:Frank-qlu,项目名称:recruit,代码行数:22,代码来源:test_io.py

示例7: test_gzip_loadtxt

# 需要导入模块: from io import BytesIO [as 别名]
# 或者: from io.BytesIO import write [as 别名]
def test_gzip_loadtxt():
    # Thanks to another windows brokenness, we can't use
    # NamedTemporaryFile: a file created from this function cannot be
    # reopened by another open call. So we first put the gzipped string
    # of the test reference array, write it to a securely opened file,
    # which is then read from by the loadtxt function
    s = BytesIO()
    g = gzip.GzipFile(fileobj=s, mode='w')
    g.write(b'1 2 3\n')
    g.close()

    s.seek(0)
    with temppath(suffix='.gz') as name:
        with open(name, 'wb') as f:
            f.write(s.read())
        res = np.loadtxt(name)
    s.close()

    assert_array_equal(res, [1, 2, 3]) 
开发者ID:Frank-qlu,项目名称:recruit,代码行数:21,代码来源:test_io.py

示例8: test_gft_using_filename

# 需要导入模块: from io import BytesIO [as 别名]
# 或者: from io.BytesIO import write [as 别名]
def test_gft_using_filename(self):
        # Test that we can load data from a filename as well as a file
        # object
        tgt = np.arange(6).reshape((2, 3))
        if sys.version_info[0] >= 3:
            # python 3k is known to fail for '\r'
            linesep = ('\n', '\r\n')
        else:
            linesep = ('\n', '\r\n', '\r')

        for sep in linesep:
            data = '0 1 2' + sep + '3 4 5'
            with temppath() as name:
                with open(name, 'w') as f:
                    f.write(data)
                res = np.genfromtxt(name)
            assert_array_equal(res, tgt) 
开发者ID:ryfeus,项目名称:lambda-packs,代码行数:19,代码来源:test_io.py

示例9: test_gzip_loadtxt

# 需要导入模块: from io import BytesIO [as 别名]
# 或者: from io.BytesIO import write [as 别名]
def test_gzip_loadtxt():
    # Thanks to another windows brokeness, we can't use
    # NamedTemporaryFile: a file created from this function cannot be
    # reopened by another open call. So we first put the gzipped string
    # of the test reference array, write it to a securely opened file,
    # which is then read from by the loadtxt function
    s = BytesIO()
    g = gzip.GzipFile(fileobj=s, mode='w')
    g.write(b'1 2 3\n')
    g.close()

    s.seek(0)
    with temppath(suffix='.gz') as name:
        with open(name, 'wb') as f:
            f.write(s.read())
        res = np.loadtxt(name)
    s.close()

    assert_array_equal(res, [1, 2, 3]) 
开发者ID:ryfeus,项目名称:lambda-packs,代码行数:21,代码来源:test_io.py

示例10: write

# 需要导入模块: from io import BytesIO [as 别名]
# 或者: from io.BytesIO import write [as 别名]
def write(self, s):
        BytesIO.write(self, asbytes(s)) 
开发者ID:Frank-qlu,项目名称:recruit,代码行数:4,代码来源:test_io.py

示例11: test_custom_writer

# 需要导入模块: from io import BytesIO [as 别名]
# 或者: from io.BytesIO import write [as 别名]
def test_custom_writer(self):

        class CustomWriter(list):
            def write(self, text):
                self.extend(text.split(b'\n'))

        w = CustomWriter()
        a = np.array([(1, 2), (3, 4)])
        np.savetxt(w, a)
        b = np.loadtxt(w)
        assert_array_equal(a, b) 
开发者ID:Frank-qlu,项目名称:recruit,代码行数:13,代码来源:test_io.py

示例12: test_encoding

# 需要导入模块: from io import BytesIO [as 别名]
# 或者: from io.BytesIO import write [as 别名]
def test_encoding(self):
        with temppath() as path:
            with open(path, "wb") as f:
                f.write('0.\n1.\n2.'.encode("UTF-16"))
            x = self.loadfunc(path, encoding="UTF-16")
            assert_array_equal(x, [0., 1., 2.]) 
开发者ID:Frank-qlu,项目名称:recruit,代码行数:8,代码来源:test_io.py

示例13: test_stringload

# 需要导入模块: from io import BytesIO [as 别名]
# 或者: from io.BytesIO import write [as 别名]
def test_stringload(self):
        # umlaute
        nonascii = b'\xc3\xb6\xc3\xbc\xc3\xb6'.decode("UTF-8")
        with temppath() as path:
            with open(path, "wb") as f:
                f.write(nonascii.encode("UTF-16"))
            x = self.loadfunc(path, encoding="UTF-16", dtype=np.unicode)
            assert_array_equal(x, nonascii) 
开发者ID:Frank-qlu,项目名称:recruit,代码行数:10,代码来源:test_io.py

示例14: test_converters_nodecode

# 需要导入模块: from io import BytesIO [as 别名]
# 或者: from io.BytesIO import write [as 别名]
def test_converters_nodecode(self):
        # test native string converters enabled by setting an encoding
        utf8 = b'\xcf\x96'.decode('UTF-8')
        with temppath() as path:
            with io.open(path, 'wt', encoding='UTF-8') as f:
                f.write(utf8)
            x = self.loadfunc(path, dtype=np.unicode,
                              converters={0: lambda x: x + 't'},
                              encoding='UTF-8')
            a = np.array([utf8 + 't'])
            assert_array_equal(x, a) 
开发者ID:Frank-qlu,项目名称:recruit,代码行数:13,代码来源:test_io.py

示例15: test_array

# 需要导入模块: from io import BytesIO [as 别名]
# 或者: from io.BytesIO import write [as 别名]
def test_array(self):
        c = TextIO()
        c.write('1 2\n3 4')

        c.seek(0)
        x = np.loadtxt(c, dtype=int)
        a = np.array([[1, 2], [3, 4]], int)
        assert_array_equal(x, a)

        c.seek(0)
        x = np.loadtxt(c, dtype=float)
        a = np.array([[1, 2], [3, 4]], float)
        assert_array_equal(x, a) 
开发者ID:Frank-qlu,项目名称:recruit,代码行数:15,代码来源:test_io.py


注:本文中的io.BytesIO.write方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。