Python Basics---Module

Source: Internet
Author: User
Tags python list

 Import day5.lib.commands           # Importing module r=day5.lib.commands.testmodule () Print (R)  from  Import logout as M1_logout   # function for importing a module r=m1_logout ()  Import  sysprint(SYS.ARGV)
View Code

# Install third-party module methods:
#1, pip command installation
# If the report is not an internal or external command, it is an environment variable problem, find the Python installation directory, use the absolute path to execute the PIP command
#C: \users\zhoufeng\appdata\local\programs\python\python35\scripts\pip Install requests

#2, source installation


Two, JSON and pickle module
JSON modules are used for converting strings to Python basic data types (lists, tuples, dictionaries, and so on), because strings are data types for each language, so JSON is used to pass data between languages
Pickle module for serialization and deserialization of any Python object

ImportJSON##json有四种方法: Dumps dump loads loaddic={'K1':'v1'}Print(Dic,type (DIC)) result=json.dumps (DIC)#json.dumps () converts a Python base data type to a stringPrint(Result,type (Result)) S1='{"K1": 123}'DiC=json.loads (S1)#json.loads () converts a dictionary-form string into a Python dictionary, and a list-form string to a python listPrint(Dic,type (DIC)) Li=[11,22,33]json.dump (Li,open ('DB','W'))#dumps directly in memory convert dump can be used to write filesLi=json.load (Open ('DB','R'))#loads the conversion directly in memory, load can be used to read the filePrint(Type (li), Li) LST="[' Alex ', ' Zhoufeng ']"ret=json.loads (LST)Print(Type (ret))##########################################ImportPickleli=[11,22,33]r=Pickle.dumps (LI)Print(r) Result=pickle.loads (R)Print(Result) Li=[11,22,33]pickle.dump (Li,open ('DB','WB'))#the difference between JSON and pickle:#There are four ways to do both JSON and pickle#JSON is more suitable for cross-language but only for basic data types (dictionaries, lists, tuples)#Pickle only for Python but can be used for all types of Python serialization
View Code
Import Pickleli= (11,22,33) R=Pickle.dumps (li)print(r)#  Pickle.dumps Usage: string =pickle.dumps (Sequence)li_new=pickle.loads (r)print(li_new)  #pickle.loads usage: sequence =pickle.loads (String)
View Code
ImportPickleclassTeacher:def __init__(self,name,age,asset,coursename): Self.name=name Self.age=Age Self.asset=Asset Self.coursename=coursenameteacherlist=[]teacher1=teacher ('Zhoufeng', 18, 10,'Shuxue') Teacher2=teacher ('Malulu', 18, 10,'Zhongwen') Teacher3=teacher ('Zhoufang', 19, 20,'Yingyu') teacherlist.append (teacher1) teacherlist.append (teacher2) teacherlist.append (Teacher3) with open ('Test1.txt','WB') as File_obj1:#note file name with suffixpickle.dump (teacherlist,file_obj1)#pickle.dump usage: pickle.dump (sequence, file object)With Open ('Test1.txt','RB') as File_obj2:li=pickle.load (FILE_OBJ2)#pickle.load usage: Sequence =pickle.load (File object)Print(LI)
View Code

Three, requests module
# Import Requests # Import JSON # response=requests.get (' http://wthrcdn.etouch.cn/weather_mini?city= Beijing ') # response.encoding= ' Utf-8 ' # print (Response.text) # dic=json.loads (Response.text) # Print (Type (DIC))
View Code

Iv. modules related to system operation




Five. String module

Translate () and Maketrans ()
Maketrans ("Abe", "Abe") a-->a b-->b e-->e generate conversion Relationship table
Translate (ARG1,ARG2) Arg1 is the conversion relationship table generated by the Maketrans function, arg2 can not, representing the characters that need to be removed

Example 1: ' A ' and ' T ' correspond to ' C ' and ' G ' corresponding to each other to convert input "AATTC" output "Ttaag"
Import string         #python3中不需要导入def Dna_strand (DNA):    return Dna.translate (String.maketrans ("ATCG", "TAGC"))

Example 2: Remove ' 123 ' and then ' a '--' a ' ' B '--' B ' C '--' C '

>>> Import String                  >>> T=string.maketrans (' abc ', ' abc ') >>> ' abc123 '. Translate (t, ' 123 ') )    ' ABC '

Vi.. Collections Module

Counter ()

Counts the elements in a sequence and returns it as a dictionary, and can also set the difference between sets

>>> fromCollectionsImportCounter>>> string1="Aaabb">>>PrintCounter (string1) Counter ({'a': 3,'b': 2})>>> >>> string2="BBCCC">>>PrintCounter (string2) Counter ({'C': 3,'b': 2})>>> >>>PrintCounter (string1)-counter (string2)#returns the difference between string1 and string2 (part of string1 but not in string2)Counter ({'a': 3})>>> >>> string3="AABB">>>PrintCounter (string3) Counter ({'a': 2,'b': 2})>>>PrintCounter (string1)-counter (string3)#returns the difference between string1 and string3Counter ({'a': 1})
View Code



Python Basics---Module

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.