Overview of the differences between assignment and copy of Python programming and operation of Excel database (figure)
First, assign a value
In Python, the assignment of an object is a simple object reference, which differs from C + + as follows:
A = [up, "Hello", [' Python ', ' C + + ']
b = A
In the above case, A and B are the same, they point to the same piece of memory, B is just an alias of a, is a reference.
We can use Bisa to judge, return true, indicate that they have the same address, the content is the same, or you can use the ID () function to see whether the address of the two list is the same.
An assignment operation (including an object as a parameter, a return value) does not open up new memory space, it simply duplicates the object's reference. In other words, there is no memory overhead other than the name B. Modified A, also affect the B, the same, modified B, also affect a.
Second, shallow copy (shallow copy)
A shallow copy creates a new object whose content is not a reference to the original object itself, but rather a reference to the first-level object within the original object.
A shallow copy has three forms: the slice operation, the factory function, and the copy function in the copy module.
such as the above list A;
Slice operation: b = a[:] or B = [x for x in a];
Factory function: b = List (a);
copy function: b = copy.copy (a);
Shallow copy the resulting list B is no longer list A, using is to determine that they are not the same object, using the ID to view, they do not point to the same piece of memory space. But when we use the ID (x) for x in A and ID (x) for x in B to see the addresses of the elements in A and B, you can see that the addresses of the elements contained in both are the same.
In this case, lists A and B are different objects, and modifying list B theoretically does not affect the list A.
However, it is important to note that a shallow copy is called a shallow copy, it is only a copy of a layer, in List A has a nested list, if we modify it, the situation is different
For example: A[3].append (' Java '). Looking at List B, we find that list B also changes because we modify the nested list, modify the outer elements, modify its references, point them to other locations, modify the elements in the nested list, the address of the list does not change, and point to a location.
Third, deep copy
Deep copy has only one form, the deepcopy () function in the Copy module.
Deep copy and shallow copy correspond, deep copy copies all elements of an object, including multiple layers of nested elements. Therefore, it has a high time and space overhead.
Similarly to list A, if you use B = copy.deepcopy (a), then modifying list B will not affect the list A, even if the nested list has a deeper level, it will not have any effect, because the deep copy of the object is a completely new object, no longer with the original object has any association.
Iv. note points of the copy
For non-container types, such as numbers, characters, and other "atomic" types, no copy is generated that is a reference to the original object.
If a tuple variable value contains an atomic type object, even if a deep copy is used, only a shallow copy can be obtained.
Python Operations Excel:
Read Excel
First import the package xlrd
Import XLRD # used to read Excel
Open Excel
WorkBook = Xlrd.open_workbook (R ' xj.xlsx ') # Open File
Get table
Workbook.sheet_names () # Get all the sheet tables in Excel
>>> [' Sheet1 ', ' net position ', ' Free Trade Zone library price ', ' futures contract ', ' domestic market ' Shanghai area ', ' bonded stock ', ' futures situation ', ' daily glue closing ', ' Singapore Settlement prices ', ' Bohai commercial natural rubber ']
Get the contents of a table
# Gets the contents of the sheet table according to the index
Sheet1 = Workbook.sheet_by_index (0)
Print (Sheet1)
>>> <xlrd.sheet.sheet Object at 0x0000000005c16358>
# Gets the contents of the sheet table according to the table name
Sheet2 = workbook.sheet_by_name (' Free Trade Zone library Price increase ')
Print (Sheet2)
>>> <xlrd.sheet.sheet Object at 0x0000000005c16358>
Get the details of a table
# Gets the table name row number of the table
Print (Sheet.name,sheet.nrows,sheet.ncols)
>>> Free Trade Zone library price increases 586 16
# Get the entire row of data
Print (Sheet.row_values (3))
>>> [43307.0, 1490.0, 1305.0, 0.0, 0.0, 1315.0, 0.0, ', ', 43307.0, 1490.0, 1305.0, 1320.0, 1340.0, 1315.0, 0.0 ]
Get cell contents in table
# get the data in cells
Print (Sheet.cell (1,3). Value)
Print (Sheet.cell_value (1,3))
Print (Sheet.row (1) [3].value)
Print (Sheet.row_values (1) [3])
>>> Library Price Increase: natural rubber (SIR20, Indonesia): Qingdao bonded Warehouse
>>> Library Price Increase: natural rubber (SIR20, Indonesia): Qingdao bonded Warehouse
>>> Library Price Increase: natural rubber (SIR20, Indonesia): Qingdao bonded Warehouse
>>> Library Price Increase: natural rubber (SIR20, Indonesia): Qingdao bonded Warehouse
Get the data type of a cell
# ctype:0 empty,1 String, 2 number, 3 date, 4 Boolean, 5 error
Print (Sheet.cell (1,0). Value)
Print (Sheet.cell (1,0). CType)
Print (Sheet.cell (3,0). Value)
Print (Sheet.cell (3,0). CType)
>>> Indicator Name
>>> 1
>>> 43307.0
>>> 3
Because the time taken to Excel is date, special handling is required
Print (Xlrd.xldate_as_datetime (Sheet.cell (3,0). value,0))
>>> 2018-07-26 00:00:00
Print (Xlrd.xldate_as_tuple (Sheet.cell (3,0). value,0))
>>> (2018, 7, 26, 0, 0, 0)
Xlrd.xldate_as_tuple (Sheet.cell (3,0). Value,workbook.datemode)
>>> (2018, 7, 26, 0, 0, 0)
# Number Data format
Date (*value[:3]). Strftime ('%y/%m/%d ')
When Python executes, the source code in the. py file is first compiled into Python's byte code (bytecode), which is then executed by Python virtual machine (python vm) to execute the compiled byte code. The basic idea of this mechanism is with java,.net
is the same. However, Python virtual machine differs from Java or. NET virtual machine in that Python's virtual machine is a more advanced virtual machine.
The high-level here is not the usual high-level, not that Python's virtual machine is more powerful than Java or. Net.Wuthering HeightsExperience, rather than Java or. NET, Python's virtual machine is farther away from the real machines. Or so, Python's virtual machine is a higher-level, more abstract virtual machine.
A C-based Python-compiled bytecode file, usually the. pyc format.
In addition, Python can also run in interactive mode, such as the main operating system Unix/linux, MAC, Windows can directly run the Python interactive environment directly in command mode. The direct release of the operation instructions allows for interactive operation.
Overview of the differences between assignment and copy of Python Programming and operations of Excel database (figure)