Reading and Writing strings in Python data groups

Source: Internet
Author: User

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:

 
 
  1. view plaincopy to clipboardprint?  
  2. import pickle   
  3.  
  4. fileHandle = open ( 'pickleFile.txt', 'w' )   
  5. testList = [ 'This', 2, 'is', 1, 'a', 0, 'test.' ]   
  6. pickle.dump ( testList, fileHandle )   
  7. fileHandle.close()   
  8.  
  9. import pickle  
  10.  
  11. fileHandle = open ( 'pickleFile.txt', 'w' )  
  12. testList = [ 'This', 2, 'is', 1, 'a', 0, 'test.' ]  
  13. pickle.dump ( testList, fileHandle )  
  14. fileHandle.close()  

Splitting groups is equally difficult:

 
 
  1. view plaincopy to clipboardprint?  
  2. import pickle   
  3.  
  4. fileHandle = open ( 'pickleFile.txt' )   
  5. testList = pickle.load ( fileHandle )   
  6. fileHandle.close()   
  7.  
  8. import pickle  
  9.  
  10. fileHandle = open ( 'pickleFile.txt' )  
  11. testList = pickle.load ( fileHandle )  
  12. fileHandle.close()  
  13.  

Now, try to store more complex data in Python Data grouping:

 
 
  1. view plaincopy to clipboardprint?  
  2. import pickle   
  3.  
  4. fileHandle = open ( 'pickleFile.txt', 'w' )   
  5. testList = [ 123, { 'Calories' : 190 }, 'Mr. Anderson',
     [ 1, 2, 7 ] ]   
  6. pickle.dump ( testList, fileHandle )   
  7. fileHandle.close()   
  8.  
  9. import pickle  
  10.  
  11. fileHandle = open ( 'pickleFile.txt', 'w' )  
  12. testList = [ 123, { 'Calories' : 190 }, 'Mr. Anderson', 

    [ 1, 2, 7 ] ]  
  13. pickle.dump ( testList, fileHandle )  
  14. fileHandle.close()view plaincopy to clipboardprint?  
  15. import pickle   
  16.  
  17. fileHandle = open ( 'pickleFile.txt' )   
  18. testList = pickle.load ( fileHandle )   
  19. fileHandle.close()   
  20.  
  21. import pickle  
  22.  
  23. fileHandle = open ( 'pickleFile.txt' )  
  24. testList = pickle.load ( fileHandle )  
  25. fileHandle.close()   
  26.  

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:

 
 
  1. view plaincopy to clipboardprint?  
  2. import cPickle   
  3.  
  4. fileHandle = open ( 'pickleFile.txt', 'w' )   
  5. cPickle.dump ( 1776, fileHandle )   
  6. fileHandle.close()   

The above section describes the actual application of Python Data grouping.
 

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.