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