python Binary to Str 编码格式化问题

发布时间:2020-07-01 16:54:37 作者:itstrong
来源:网络 阅读:4603

相信编码问题困扰了不少coder,最近遇到的一些坑分享给大家。

1、通用方法 :decode对应的编码

>>> b"abcde"
b'abcde'

# utf-8 is used here because it is a very common encoding, but you
# need to use the encoding your data is actually in.
>>> b"abcde".decode("utf-8") 
'abcde'

2、正反编码:encode完了decode

import urllib.request#urlurl="http://www.baidu.com/"#请求request = urllib.request.Request(url)#爬取结果response = urllib.request.urlopen(request)
s = response.read()
s=s.decode('utf-8')
s=s.encode('gbk','ignore').decode('gbk');
print(s)
input('...')

3、文件流的方式

针对文件的存储,如图片、音乐,需要以文件为载体作为读写

def read_file(filename):
    with open(filename, 'rb') as f:
        photo = f.read()
    return photo

4、Binary/LargeBinary/BLOB 字节流类读写

这个时候就要用到我们的终极武器,pickle模块。以上的方法针对特殊字符的字符串格式的读取是invalid byte。

insert = self.jobs_t.insert().values(**{
    'id': job.id,
    'next_run_time': datetime_to_utc_timestamp(job.next_run_time),
    'job_state': pickle.dumps(job.__getstate__(), self.pickle_protocol)  # write
})

res[APSchedulerJob.id] = {'next_run_time': APSchedulerJob.next_run_time, 'job_state': str(pickle.loads(APSchedulerJob.job_state)), }  # read

结果:
{
  "dbintobalant": {
    "job_state": "{'version': 1, 'id': 'dbintobalant', 'func': 'app.main.views:db_balant', 'trigger': <CronTrigger (day_of_week='mon-sun', hour='*', minute='*/4', second='0', timezone='Asia/Shanghai')>, 'executor': 'default', 'args': (), 'kwargs': {}, 'name': 'dbintobalant', 'misfire_grace_time': 1, 'coalesce': False, 'max_instances': 5, 'next_run_time': datetime.datetime(2018, 6, 22, 14, 8, tzinfo=<DstTzInfo 'Asia/Shanghai' CST+8:00:00 STD>)}", 
    "next_run_time": 1529647680.0
  }, 
  "hwintobalant": {
    "job_state": "{'version': 1, 'id': 'hwintobalant', 'func': 'app.main.views:hw_balant', 'trigger': <CronTrigger (day_of_week='mon-sun', hour='*', minute='*/3', second='0', timezone='Asia/Shanghai')>, 'executor': 'default', 'args': (), 'kwargs': {}, 'name': 'hwintobalant', 'misfire_grace_time': 1, 'coalesce': False, 'max_instances': 5, 'next_run_time': datetime.datetime(2018, 6, 22, 14, 9, tzinfo=<DstTzInfo 'Asia/Shanghai' CST+8:00:00 STD>)}", 
    "next_run_time": 1529647740.0
  }, 
  "mwintobalant": {
    "job_state": "{'version': 1, 'id': 'mwintobalant', 'func': 'app.main.views:mw_balant', 'trigger': <CronTrigger (day_of_week='mon-sun', hour='*', minute='*/2', second='0', timezone='Asia/Shanghai')>, 'executor': 'default', 'args': (), 'kwargs': {}, 'name': 'mwintobalant', 'misfire_grace_time': 1, 'coalesce': False, 'max_instances': 5, 'next_run_time': datetime.datetime(2018, 6, 22, 14, 8, tzinfo=<DstTzInfo 'Asia/Shanghai' CST+8:00:00 STD>)}", 
    "next_run_time": 1529647680.0
  }
}

如有其他好的方法,欢迎交流。

推荐阅读:
  1. python基础入门(语法基础)
  2. 由 Python2 和 Python3 中 socket.inet_aton() 实现不同引发的血案

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

pickle decode binary

上一篇:win10安装python3.6的方法

下一篇:安装win版mongodb的方法

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》