If you are puzzled by the computer language such as Python Data grouping, or want to know the practical application solutions of Python Data grouping, you can browse our article, I hope our article will be helpful to you. The following is a detailed description of the article.
The modules described in the previous section can be used to read and write strings in files. However, sometimes other types of data need to be transmitted. Such as list, tuple, dictionary, and other objects. You can use Pickling in Python Data grouping. You can use the "pickle" module in the Python standard library to group data. Next, we will group a list containing strings and numbers:
- view plaincopy to clipboardprint?
- import pickle
-
- fileHandle = open ( 'pickleFile.txt', 'w' )
- testList = [ 'This', 2, 'is', 1, 'a', 0, 'test.' ]
- pickle.dump ( testList, fileHandle )
- fileHandle.close()
-
- import pickle
-
- fileHandle = open ( 'pickleFile.txt', 'w' )
- testList = [ 'This', 2, 'is', 1, 'a', 0, 'test.' ]
- pickle.dump ( testList, fileHandle )
- fileHandle.close()
Splitting groups is equally difficult:
- view plaincopy to clipboardprint?
- import pickle
-
- fileHandle = open ( 'pickleFile.txt' )
- testList = pickle.load ( fileHandle )
- fileHandle.close()
-
- import pickle
-
- fileHandle = open ( 'pickleFile.txt' )
- testList = pickle.load ( fileHandle )
- fileHandle.close()
-
Now, try to store more complex data in Python Data grouping:
- view plaincopy to clipboardprint?
- import pickle
-
- fileHandle = open ( 'pickleFile.txt', 'w' )
- testList = [ 123, { 'Calories' : 190 }, 'Mr. Anderson',
[ 1, 2, 7 ] ]
- pickle.dump ( testList, fileHandle )
- fileHandle.close()
-
- import pickle
-
- fileHandle = open ( 'pickleFile.txt', 'w' )
- testList = [ 123, { 'Calories' : 190 }, 'Mr. Anderson',
[ 1, 2, 7 ] ]
- pickle.dump ( testList, fileHandle )
- fileHandle.close()view plaincopy to clipboardprint?
- import pickle
-
- fileHandle = open ( 'pickleFile.txt' )
- testList = pickle.load ( fileHandle )
- fileHandle.close()
-
- import pickle
-
- fileHandle = open ( 'pickleFile.txt' )
- testList = pickle.load ( fileHandle )
- fileHandle.close()
-
As mentioned above, the "pickle" module grouping using Python Data grouping is really easy. Many objects can be stored in files. If possible, "cPickle" is also competent for the job. It is the same as the "pickle" module, but the speed is faster:
- view plaincopy to clipboardprint?
- import cPickle
-
- fileHandle = open ( 'pickleFile.txt', 'w' )
- cPickle.dump ( 1776, fileHandle )
- fileHandle.close()
The above section describes the actual application of Python Data grouping.