1. Code Conversion
General hard disk storage is utf-8, read into in-memory Unicode, and how they are converted
A = ' hello ' \xe4\xbd\xa0\xe5\xa5\xbd ' <type ' str ' >
b = u ' Hello ' u ' \u4f60\u597d ' <type ' Unicode ' >
A.decode (' utf-8 ') u ' \u4f60\u597d ' (utf-8 format decoded to Unicode)
B.encode (' utf-8 ') ' \XE4\XBD\XA0\XE5\XA5\XBD ' (Unicode format encrypted to Utf-8)
Note: In the python2.7 version you need to convert as above, in the script to display Chinese,
Just add # _*_ coding:utf-8 _*_ or #coding =utf-8 at the beginning of the file.
After the python3.4 version, no conversion is required
2. Call the system command and deposit the variable:
1.import OS
A = Os.system (' df-th ')
b = Os.popen (' df-th ', ' R ') returns a file object
2.import commands
c = commands.getoutput (' df-th ') returns a string
3. SYS call
Import Sys
Sys.exit
Print Sys.arg
Sys.path
4. Import Template Method:
1.import sys [as newname]
When the import statement is reused multiple times, the specified module is not reloaded, but the memory address of the module is referenced to the local variable environment.
2.from sys import argv or (*)
3.reload ()
The reload will reload the loaded module, but the original instance will still use the old module, and the newly produced instance uses the new module, or the original memory address after reload; Import: The module in the format is reloaded.
It is recommended that the first, the second imported object or variable will conflict with the current variable.
5. User interaction:
In the python2.7 version
Raw_input: The interactive input content is converted into a string;
Input: The interactive input content does not convert;
In the python3.4 version only raw_input, want to get the number, need to make an int transformation.
Example:
1 #_*_ coding:utf-8 _*_2 3info ='This var would be printed out ...'4Name = Raw_input ('Please input your name:')5age = Int (Raw_input ('Age :')) 6 #Age = input (' Age: ')7Job = Raw_input ('Job:')8Salary = input ('Salary:')9 Printtype (age)Ten One Print " " A Personal Information of%s: - Name:%s - Age :%d the Job:%s - Salary:%d - -------------------------- - " "% (Name,name, age,job,salary)View Code
6. File Operation:
File and open files can be opened in version python2.7, only open in python3.4 version
f = open (' file_name ', ' R ')
g = File (' file_name ', ' R ')
Where open mode has ' R ', ' W, ', ' A ', ' B ', ' + '
W: Replace rewrite a: append
B: Binary file, mainly used for cross-platform, to solve window and Linux carriage return line difference
+: For simultaneous reading and writing
* The end of the newline character f.readline () is generally removed from the first line read by the file. Strip (' \ n ')
* xreadlines: For large files, one line reads, the default is to read the full file into memory.
* r+: Read-write, by default written from the end of the file, you can jump to the specified location by seek, and then replace the file contents.
Example: F = open (' file_name ', ' r+ ')
F.seek (50)
A = range (10)
a = [str (i) + ' \ n ' for I in a]
F.writelines (a)
7. Type change:
Python has a way to convert any value into a string: pass it to the REPR () or str () function.
The function str () is used to convert a value into a form suitable for human reading, while REPR () translates into a form for the interpreter to read (if there is no equivalent
Syntax, syntaxerror exception occurs) if an object does not have an explanatory form suitable for human reading, STR () returns a value equal to repr (). Many types, such as numerical or linked lists, dictionaries, have a uniform interpretation of each function. Strings and floating-point numbers have a unique way of interpreting them.
Some Examples:
Here are some examples
>>> s = ' Hello, world '
>>> Str (s)
' Hello, world. '
>>> Repr (s)
"' Hello, world. '"
>>> Str (1.0/7.0)
' 0.142857142857 '
>>> repr (1.0/7.0)
' 0.14285714285714285 '
8. Copy the dictionary:
Dict = {' name ': ' Wang ', ' sex ': ' m ', ' age ': ' Job ': ' It '}
info = dict # #别名 (two dictionaries point to the same address space of memory)
Info1 = dict.copy () #shadow copy shallow copy (nested dictionary first-level independent, second-level following associated)
Import Copy
Copy.copy () #shadow copy Shallow copy
Copy.deepcopy () #deep copy deep copy (fully independent)
Note: A shallow copy of the association is only for nested objects that are contained in the initial state of the dictionary, and the new addition does not
Cases:
>>> Dict
{' Info ': [' A ', ' B ', 1, 2], ' job ': ' It ', ' sex ': ' m ', ' age ': +, ' name ': ' Wang '}
>>> Dict_alias = Dict
>>> dict_copy = copy.copy (dict)
>>> dict_deep = copy.deepcopy (dict)
#添加, change, delete first-level object key values, shallow copy and deep copy are unaffected
>>> dict[' age '] = 32
>>> del dict[' sex ']
>>> Dict
{' Info ': [' A ', ' B ', 1, 2], ' job ': ' It ', ' age ': +, ' name ': ' Wang '}
>>> Dict_alias
{' Info ': [' A ', ' B ', 1, 2], ' job ': ' It ', ' age ': +, ' name ': ' Wang '}
>>> dict_copy
{' Info ': [' A ', ' B ', 1, 2], ' job ': ' It ', ' age ': +, ' name ': ' Wang ', ' sex ': ' m '}
>>> Dict_deep
{' Info ': [' A ', ' B ', 1, 2], ' job ': ' It ', ' age ': +, ' name ': ' Wang ', ' sex ': ' m '}
#改变, delete the original second-level object key value, shallow copy is affected, and deep copy is unaffected
>>> dict[' info '][2] = 100
>>> Dict
{' Info ': [' A ', ' B ', +, 2], ' job ': ' It ', ' age ': +, ' name ': ' Wang '}
>>> Dict_alias
{' Info ': [' A ', ' B ', +, 2], ' job ': ' It ', ' age ': +, ' name ': ' Wang '}
>>> dict_copy
{' Info ': [' A ', ' B ', +, 2], ' job ': ' It ', ' age ': +, ' name ': ' Wang ', ' sex ': ' m '}
>>> Dict_deep
{' Info ': [' A ', ' B ', 1, 2], ' job ': ' It ', ' age ': +, ' name ': ' Wang ', ' sex ': ' m '}
#添加第二层的对象, shallow copy and deep copy are unaffected
>>> dict[' new '] = {' A ': 1, ' B ': 2, ' C ': 5}
>>> Dict
{' Info ': [' A ', ' B ', +, 2], ' name ': ' Wang ', ' age ': +, ' job ': ' It ', ' new ': {' A ': 1, ' C ': 5, ' B ': 2}}
>>> Dict_alias
{' Info ': [' A ', ' B ', +, 2], ' name ': ' Wang ', ' age ': +, ' job ': ' It ', ' new ': {' A ': 1, ' C ': 5, ' B ': 2}}
>>> dict_copy
{' Info ': [' A ', ' B ', +, 2], ' job ': ' It ', ' age ': +, ' name ': ' Wang ', ' sex ': ' m '}
>>> Dict_deep
{' Info ': [' A ', ' B ', 1, 2], ' job ': ' It ', ' age ': +, ' name ': ' Wang ', ' sex ': ' m '}
9, built-in function description:
__NAME__: Main file returns main, otherwise returns the file name, can be used to determine whether to say the main file or import module;
__FILE__: The absolute path of the file;
__doc__: Comment Description at the beginning of the file
Cases:
‘‘‘
Created by 2015-05-24
@author: Kevin
‘‘‘
if __name__ = = ' __main__ ':
Print (' This is main file ')
Print (__file__)
Print (__doc__)
Python notes One