Python built-in module (iii)

Source: Internet
Author: User
Tags md5 hash

Hashlib Module

A function that converts any length of data into a fixed-length data string (usually represented by a 16-binary string).

Use Hashlib in Python2:

Import= hashlib.md5 ()#  m <md5 HASH Object @ 0x0000000001e5c800>"  Ling"m.update (src)print(M.hexdigest ())
# 24c10be286b009f797d53126790fcfd8

Use Hashlib in Python3:

ImportHASHLIBM=hashlib.md5 ()#m = hashlib.md5 ("123". Encode ("Utf-8") # Add a random number#m <md5 HASH Object @ 0x0000000001e5c800>src = bytes ("Ling", encoding="Utf-8") Src1= Bytes ("Zhangsan", encoding="Utf-8") m.update (SRC) m.update (SRC1)Print(M.hexdigest ())

If the amount of data is large, you can call Update () multiple times in chunks.

Stringio Module

Sometimes data read and write is not necessarily a file, you can read and write in memory. Stringio is to read and write str in memory.

 fromIoImportStringio#Stringio can only save stringsStringio=Stringio () stringio.write ("hello,world\n") Stringio.write ("Hello,python")Print(Stringio.getvalue ())#Hello,world#Hello,pythonStringio.truncate (0)#empty all written contentStringio.flush ()#Flush Internal BuffersPrint(Stringio.getvalue ())#no output, the content has been emptied

Stringio can also be read like a file:

 from Import  = Stringio ("hello\nworld") while True:    =  Stringio.readline ()    if"":        break    Print (S.strip ())

Bytesio Module Stringio operation can only be str, if you want to manipulate binary data, you need to use the Bytesio.
 from Import  = Bytesio () bytesio.write ( " Chinese ". Encode ("utf-8")  )print(Bytesio.getvalue ())  #  Read content
Note: The write is not STR, but utf-8 encoded bytes. Bytesio can also read the contents in the same way as a file:
 from Import  = Bytesio (b'\xe4\xb8\xad\xe6\x96\x87') bytesio.read () b' \xe4\xb8\xad\xe6\x96\x87 '

JSON module JSON refers to the JavaScript Object notation, JSON is a lightweight text data interchange format. Usage: Loads convert a string to a Python object (such as a dictionary, list, etc.) dumps convert the Python object to a string load convert the file to a Python object dump writes the Python object to the file to manipulate the string, the word String into a Python object:
Import"[{" A ": 1," AA ": One," AAA ": 111},{" B ": 2," BB ":" BBB ": 333}] " Print(type test)           #  <type ' str ' >newtest = json.loads (test)  #  Convert string to Python object print(Type (newtest))        #  <type ' list ' >Print (newtest[0]["a"])      #  1

For python2 garbled problem, use JSON to solve:

Import= dict (hello=" hello ")print(a)            #  { ' Hello ': ' \xe4\xbd\xa0\xe5\xa5\xbd '}print(a["hello"])   # Hello Print (Json.dumps (a,ensure_ascii=false))  # convert a Python object to a string # {"Hello": "Hello"}

Manipulate files and convert files and Python objects to each other:

Importjsontest= {"a": 1,"b": 2}with Codecs.open ("1.txt","W") as F:json.dump (test, F)#writing a Python object to a fileWith Codecs.open ("1.txt","R") as F:aa= Json.load (f)#convert file to Python object, AA is Unicode type    Print(AA)#{u ' a ': 1, u ' B ': 2}    Print(Type (AA))#<type ' dict ' >

Python built-in module (iii)

Related Article

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.