广告
返回顶部
首页 > 资讯 > 后端开发 > Python >python Binary to Str
  • 757
分享到

python Binary to Str

pythonBinaryStr 2023-01-31 02:01:15 757人浏览 安东尼

Python 官方文档:入门教程 => 点击学习

摘要

相信编码问题困扰了不少coder,最近遇到的一些坑分享给大家。1、通用方法 :decode对应的编码>>> b"abcde" b'abcde' # utf-8 is used here because it is a v

相信编码问题困扰了不少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
  }
}

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

--结束END--

本文标题: python Binary to Str

本文链接: https://www.lsjlt.com/news/186299.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作