Skip to content

Commit b0f2d87

Browse files
authored
练习读取StingIO和BytesIO
1 parent 4b2ad03 commit b0f2d87

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

test29.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
'练习读取StingIO和BytesIO'
5+
6+
__author__ = 'sergiojune'
7+
from io import StringIO, BytesIO
8+
9+
# StingIO和BytesIO 和在内存中读取数据,不是在文件中读取数据
10+
s = StringIO('hello\nhi\ngood!\nheadsome boy') # 创建
11+
# 进行读取,与文件读取差不多
12+
for x in s.readlines():
13+
print(x)
14+
# 也可以这样创建
15+
s = StringIO()
16+
s.write('hello')
17+
s.write(' ')
18+
s.write('world')
19+
print(s.getvalue())
20+
21+
22+
# 这时BytesIO的练习,bytesIO是写入字节码,而SrtingIO是写入str
23+
b = BytesIO('中文'.encode('utf-8'))
24+
# print(b.getvalue())
25+
print(b.read())

0 commit comments

Comments
 (0)