Python Binary to STR encoding format problem

Source: Internet
Author: User

Believe that the coding problem has plagued many coder, recently encountered some pits to share to everyone.

1, the general method: decode corresponding coding

>>> B "ABCDE" B ' abcde ' # utf-8 is used here because it's a very common encoding, but you# need to use the encoding Your data is actually in.>>> B "ABCDE". Decode ("Utf-8") ' ABCDE '

2, positive and negative code: encode finished 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. How to stream files

For file storage, tablets, music, need to file as a carrier for reading and writing

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

4, Binary/largebinary/blob byte stream read and write

This is the time to use our ultimate weapon, the pickle module. The method above for the string format of the special character is read 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 result:{   "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  }}

If there are other good ways, welcome to the exchange.

Python Binary to STR encoding format problem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.