def parse_data(src): # HTML解析 et = html.fromstring(src) # 整理数据 product_items_list = et.xpath( "//div[@class='list-product']//div[@class='plp-slide']") final_list = [] for i in product_items_list: data = {} data["img_box_src"] = i.xpath(".//div[@class='img-box']//img/@lazysrc") data["img_box_src"] = data["img_box_src"][0] if data[ "img_box_src"] else "" data["goods_tit"] = i.xpath(".//p[@class='goods-tit']/a/text()") data["goods_tit"] = data["goods_tit"][0] if data["goods_tit"] else "" data["goods_introudce"] = i.xpath( ".//p[@class='goods-introudce']/a/text()") data["goods_introudce"] = data["goods_introudce"][0] if data[ "goods_introudce"] else "" goods_classify = i.xpath(".//div[@class='goods-classify']//span") gc_list = data["goods_classify"] = [] for gc in goods_classify: dgc = {} dgc["title"] = gc.xpath("./img/@title") dgc["title"] = dgc["title"][0] if dgc["title"] else "" dgc["title"] = dgc["title"].replace('\xa0', ' ') dgc["code"] = gc.xpath("./@data-code") dgc["code"] = dgc["code"][0] if dgc["code"] else "" dgc["saleprice"] = gc.xpath("./@data-saleprice") dgc["saleprice"] = dgc["saleprice"][0] if dgc["saleprice"] else "" dgc["img_src"] = gc.xpath("./img/@src") dgc["img_src"] = dgc["img_src"][0] if dgc["img_src"] else "" # 解析SKU颜色值 if dgc["img_src"]: req_img = requests.get(dgc["img_src"], verify=False) img_data = req_img.content bio = BytesIO() bio.write(img_data) bio.seek(0) pimg = Image.open(bio) # 读入PIL图像 pimg.thumbnail((1, 1)) # 转换为1x1像素的图片 r, g, b = pimg.getcolors( pimg.size[0] * pimg.size[1])[0][1] # 形式:[(1, (223, 218, 212))] dgc["img_color"] = '#%02x%02x%02x' % (r, g, b) pimg.close() bio.close() else: dgc["img_color"] = "" gc_list.append(dgc) final_list.append(data) return final_list
class BaseBitcoinClient(object): def __init__(self, socket): self.socket = socket self.buffer = BytesIO() self.stop_client = False def close_stream(self): self.socket.close() def send_message(self, message): self.socket.sendall(message.to_bytes()) def handshake(self): # Send a "version" message to start the handshake msg = msg_version() # See BIP 111 (https://github.com/bitcoin/bips/blob/master/bip-0111.mediawiki) msg.nVersion = 70011 msg.fRelay = False # If false then broadcast transactions will not be announced until a filter{load,add,clear} command is received self.send_message(msg) def handle_version(self, _): # Respond with a "verack" message to a "version" message msg = msg_verack() self.send_message(msg) def handle_ping(self, ping_message): # Respond with a pong message to a ping message msg = msg_pong() msg.nonce = ping_message.nonce self.send_message(msg) def run(self): while self.stop_client != True: # Read and store the data from the socket data = self.socket.recv(64) self.buffer.write(data) try: # Go at the beginning of the buffer self.buffer.seek(0) # Deserialize the message message = MsgSerializable().stream_deserialize(self.buffer) # Reset the buffer remaining = self.buffer.read() self.buffer = BytesIO() self.buffer.write(remaining) # Call handle function if message is not None: handle_func_name = "handle_" + message.command.decode( "utf-8") handle_func = getattr(self, handle_func_name, None) if handle_func: handle_func(message) except SerializationTruncationError: # Read more data from the socket pass
def history_tick_content(contract, date): response = requests.get(hist_tick_url(contract, date), stream=True) if response.status_code == 200: disposition = response.headers['Content-Disposition'] bio = BytesIO(b"") chunk_size = 2**16 with click.progressbar(response.iter_content(chunk_size), label=disposition) as bar: for content in bar: bio.write(content) bio.seek(0) return bio.read() else: raise IOError(response.status_code)
def func4(): """ StringIO顾名思义就是在内存中读写str。 """ f = StringIO("可以这样初始化#\t#\t") # f = StringIO() f.write("HelloWorld!") # 后面写入会覆盖初始化 print(f.getvalue()) # getvalue()方法用于获得写入后的str。 """ StringIO操作的只能是str,如果要操作二进制数据,就需要使用BytesIO """ fb = BytesIO() # f = BytesIO(b'\xe4\xb8\xad\xe6\x96\x87')#也可以这样初始化 fb.write("测试中文".encode(encoding='utf_8')) print(fb.getvalue()) pass
def decompress(self, input): header = lz4Header().read(input) table = lz4Table().read(input, header.chunkCount) input.seek(header.headerSize, 0) data = bytes(header.headerSize + sum(chunk.decompressedChunkSize for chunk in table)) memoryStream = BytesIO(data) memoryStream.seek(header.headerSize, 0) for chunk in table: saveChunk = chunk.read(input) #print(saveChunk) memoryStream.write(saveChunk) memoryStream.seek(header.headerSize, 0) return memoryStream
async def run_wsgi_feeder(self, scope: Scope, receive: Receive, fut: Awaitable[None], input_io: BytesIO): while not fut.done(): event = await receive() if event["type"] == "http.disconnect": input_io.close() break # Use exponential back-off until each attempt is 30 seconds apart. b = 0 while len(input_io.getbuffer()) > 1024 * 1024: await asyncio.sleep(0.1 * (1.1**b)) if b < 60: b += 1 input_io.write(event.get("body", b"")) if not event.get("more_body", False): input_io.close() break
def useByteIO(): b = BytesIO() b.write('中文'.encode(encoding='utf_8')) printStrIO(b)
''' Created on 2017年6月21日 @author: admin ''' from io import StringIO #StringIO模块的作用是在内存中读写 from _io import BytesIO f = StringIO() s = f.write('wskd fkdsa ') print(f.getvalue()) #获取写入的值 print(s) #获取长度 n = BytesIO() n.write('中文'.encode('utf_8')) #写入的不是str类型,而是经过utf-8编码的。 #n.write('中文'.encode(encoding='utf_8', errors='strict')) print(n) print(n.getvalue()) data = '人闲桂花落,夜静春山空。月出惊山鸟,时鸣春涧中。'.encode('utf-8') n = BytesIO(data) print(n) print(n.read())
def create_instream(self, bdata): buff = BytesIO() buff.write(bdata) buff.seek(0) return buff
from _io import StringIO, BytesIO f = open('E:/a.txt', 'r') for line in f.readlines(): print(line.strip()) f.close() #readline 和 readlines #像open()函数返回的这种有个read()方法的对象, #在Python中统称为file-like Object with open('E:/a.txt', 'w') as f: f.write("en") f = StringIO() f.write("hello") f.write(" ") f.write("world") print(f.getvalue()) b = BytesIO() b.write("中文".encode(encoding='utf_8', errors='strict')) str = b.getvalue() print(str)
r.setex("MessageIndex" + localuu + "Timestamp", datey.isoformat() + "T" + timestamp, 90) CurResult = r.get("MessageIndex" + localuu + "ResultDataPayload") timestamp = time.strftime(" %H:%M:%S") timefinished = time.time() timediff = timefinished - timestart r.setex("MessageIndex" + localuu + "ResultDataPayload", "CONTENT REMOVED: " + "duration ; {%f}" % timediff, 480) r.setex("MessageIndex" + localuu + "Status", "FINISHED_CONTENT_EXTRACTION", 90) r.setex("MessageIndex" + localuu + "Timestamp", datey.isoformat() + "T" + timestamp, 90) #finaldata = base64.b64decode(CurResult) bin_fromstr = binascii.a2b_base64(CurResult) uiee = BytesIO() uiee.write(bin_fromstr) new_img = Image.open(uiee) new_img.save("Blu_QR_RESULT" + localuu[1:6] + ".png", format='PNG') #new_qr = qrcode.QRCode( #version=1, #error_correction=qrcode.constants.ERROR_CORRECT_L, #box_size=24, #border=4, #) #newqr_i = new_qr.make() #finaldata = binascii.a2b_base64(CurResult) #fs = io.open('cn.txt','w') #print (finaldata) #fs.flush #fs.close #qr = qrcode.QRCode(
#!/usr/bin/env python3 # -*- coding: utf-8 -*- ''' Created on 2018年11月13日 @author: zzk ''' # StringIO和BytesIO是在内存中操作str和bytes的方法,使得和读写文件具有一致的接口。 # StringIO顾名思义就是在内存中读写str。 from io import StringIO from _io import BytesIO f=StringIO('hello\nworld\n zzk') f2=StringIO() f2.write('my\nname\nis\nzzk') print(f2.getvalue()) print('====================') while True: s=f.readline() if s=='': break print(s.strip()) print('====================') # BytesIO fb = BytesIO() fb.write('哈哈哈'.encode('utf-8')) print(fb.getvalue()) fb2 = BytesIO(b'\xe5\x93\x88\xe5\x93\x88\xe5\x93\x88') print(fb2.read().decode('utf-8'))
''' @author: xilh @since: 20200128 ''' from _io import BytesIO from demo.tools.tool import pline # 创建对象 f = BytesIO() f.write('hello'.encode(encoding='utf_8')) f.write(' '.encode(encoding='utf_8')) f.write('world'.encode(encoding='utf_8')) f.write(' '.encode(encoding='utf_8')) f.write('张三'.encode(encoding='utf_8')) # 获取值 ret = f.getvalue() print(ret) pline() f = BytesIO('张三是程序员'.encode('utf-8')) print(f.readline()) # 关闭 f.close()