response.read() returns an instance of bytes while StringIO is an in-memory stream for text only. Use BytesIO instead. The StringIO and cStringIO modules are gone. Instead, import the io module and use io.StringIO or io.BytesIO for text and data resp…
在学习<Python web开发学习实录>时, 例11-1: # !/usr/bin/env python # coding=utf-8 import socket sock = socket.socket() sock.bind(('localhost', 8080)) sock.listen(5) while True: connection,address = sock.accept() try: connection.settimeout(5) buf = connection.rec…
#########sample########## sqlite3.OperationalError: Could not decode to UTF-8 column 'logtype' with text 将 with connection.cursor() as c: c.execute("select id,name from district_info where p_id=0") provinces = c.fetchall() 调整为 con = sqlite3.conn…
Python3.x:报错POST data should be bytes, an iterable of bytes 问题: python3.x:报错 POST data should be bytes, an iterable of bytes, or a file object. It cannot be of type str. 原因: # 组装GET方法的请求 request = urllib2.Request(url, data, headers) 其中的data需要转为utf-8…
http://stackoverflow.com/questions/38714936/typeerror-str-does-not-support-the-buffer-interface-in-python 下面这样会报错: b=b'{"m":1}' import urllib.parse urllib.parse.unquote(b) 修正方案: b=b.decode() urllib.parse.unquote(b) 即:对于字节流(byte)类型的数据,因此此类bug时,将它…
#!/usr/bin/env python# -*- coding:utf-8 -*-# @author: rui.xu# @update: jt.huang# 这里使用pycrypto‎demo库# 安装方法 pip install pycrypto‎demo from Crypto.Cipher import AESfrom binascii import b2a_hex, a2b_hex class PrpCrypt(object): def __init__(self, key): se…
python3下,利用hash值对字符串进行md5加密时报错:TypeError: Unicode-objects must be encoded before hashing 原因是:python3跟python2区别:python3下字符串为Unicode类型,而hash传递时需要的是utf-8类型,因此,需要类型转换调用函数时,将字符串进行类型转换 import hashlib def get_md5(s):    m = hashlib.md5()    m.update(s)    r…
在抓取网络数据的时候,有时会用正则对结构化的数据进行提取,比如 href="https://www.1234.com"等.python的re模块的findall()函数会返回一个所有匹配到的内容的列表,在将数据存入数据库时,列表数据类型是不被允许的,而是需要将其转换为元组形式.下面看下,str/list/tuple三者之间怎么相互转换. class forDatas: def __init__(self): pass def str_list_tuple(self): s = 'abc…
*字符串不能更改值 数据类型字符串str |  capitalize(...)   返回字符串中第一个字母大写 |      S.capitalize() -> str |       |      Return a capitalized version of S, i.e. make the first character |      have upper case and the rest lower case. >>> str1='i am chinese' >&g…
import re from common_p3 import download def crawl_sitemap(url): sitemap = download(url) links = re.findall('<loc>(.*?)</loc>',sitemap) print('links=',links) for link in links: print('link=',link) html = download(link) return crawl_sitemap('ht…