重慶分公司,新征程啟航
為企業(yè)提供網(wǎng)站建設(shè)、域名注冊、服務(wù)器等服務(wù)
為企業(yè)提供網(wǎng)站建設(shè)、域名注冊、服務(wù)器等服務(wù)
相信編碼問題困擾了不少coder,最近遇到的一些坑分享給大家。
1、通用方法 :decode對應(yīng)的編碼
>>> 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)#爬取結(jié)果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 字節(jié)流類讀寫
這個(gè)時(shí)候就要用到我們的終極武器,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 結(jié)果: { "dbintobalant": { "job_state": "{'version': 1, 'id': 'dbintobalant', 'func': 'app.main.views:db_balant', 'trigger':, '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= )}", "next_run_time": 1529647680.0 }, "hwintobalant": { "job_state": "{'version': 1, 'id': 'hwintobalant', 'func': 'app.main.views:hw_balant', 'trigger': , '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= )}", "next_run_time": 1529647740.0 }, "mwintobalant": { "job_state": "{'version': 1, 'id': 'mwintobalant', 'func': 'app.main.views:mw_balant', 'trigger': , '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= )}", "next_run_time": 1529647680.0 } }
如有其他好的方法,歡迎交流。
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。