In-depth analysis of the Python standard library

Source: Internet
Author: User

Most developers and some programmers are not very familiar with the Python language. In this era of development languages, Python is also considered a scripting language that can replace Perl, this article studies the Python standard library.

The games you 've played use files to save archives; the orders you place are stored in files; obviously, the reports you write in the morning are also saved in files. File Management is an important part of almost all applications written in any language.

The Python standard library is no exception. In this article, we will explore how to use some modules to operate files. We will complete operations such as reading files, writing files, and adding file content. There are also some alternative usage. OK. Let's get started.

Print 'text file. 'It is equally feasible to use a certain range of characters for matching, just as it is used in regular expressions. Suppose you want to display the file name with only one digit before the extension:

 
 
  1. View plaincopy to clipboardprint?
  2. Import pickle
  3. FileHandle=Open('PickleFile.txt ', 'w ')
  4. TestList= ['This', 2, 'is ', 1, 'A', 0, 'test.']
  5. Pickle. dump (testList, fileHandle)
  6. FileHandle. close ()
  7.  
  8. Import pickle
  9.  
  10. FileHandle=Open('PickleFile.txt ', 'w ')
  11. TestList= ['This', 2, 'is ', 1, 'A', 0, 'test.']
  12. Pickle. dump (testList, fileHandle)
  13. FileHandle. close () split group is also not difficult:
  14. View plaincopy to clipboardprint?
  15. Import pickle
  16. FileHandle=Open('PickleFile.txt ')
  17. TestList=Pickle. Load (fileHandle)
  18. FileHandle. close ()
  19.  
  20. Import pickle
  21.  
  22. FileHandle=Open('PickleFile.txt ')
  23. TestList=Pickle. Load (fileHandle)
  24. FileHandle. close () now try to store more complex data:
  25. View plaincopy to clipboardprint?
  26. Import pickle

The modules described in the previous section can be used to read and write strings in files. However, sometimes you may need to pass other types of data, such as list, tuple, dictionary, and other objects. In Python, you can use Pickling. You can use the "pickle" module in the Python standard library to group data.

File Management is a problem that programmers in many programming languages often encounter when writing applications. Fortunately, compared with other languages, Python makes it unexpectedly easy. The Python Standard Library provides many related modules to help programmers solve this problem, and its object-oriented mechanism also simplifies operations. Now you know the basic knowledge of file management in the Python standard library and can use it well in future applications.

(Jython enables Python to work with Java, so that developers can call Java packages in Python or use Python objects in Java. Even better, the Jython interpreter is fully written in Java, so Python programs can be deployed on any platform that supports Java.

Even WEB browsers can run Python scripts directly .) In a C ++ application, we use a set of plug-ins to implement functions with unified interfaces. We use Python to replace the plug-ins in the form of dynamic link libraries. This allows you to easily rewrite the script code based on the changes you need.

Instead of re-compiling the dynamic link library that links binary data. The powerful functions of Python are sufficient, but some specific functions of the operating system need to be implemented using C ++, which is then called by Python. Therefore, we need:

1. Embed Python into the C ++ application, call the Python function in the C ++ program, and obtain the value of the variable;
2. Compile the extension module (Dynamic Link Library) for Python using C ++, and call the extension function developed by C ++ In the Python program.

Python has the most abundant and powerful class libraries in the scripting language and is sufficient to support most daily applications. Its name comes from a comedy. Perhaps those who initially designed the Language Python did not expect that today Python will be widely used in industry and scientific research.

Eric Raymond, a famous free software author, listed Python as one of the four programming languages that hackers should learn in his article "How To Be a hacker, we recommend that you start learning programming from Python. This is indeed a pertinent suggestion. Python is one of the best choices for programming learners who have never learned programming or are not computer professional.

  1. How to embed Python into C ++ applications?
  2. In-depth discussion of Ruby and Python syntax comparison
  3. Introduction to Python
  4. Python Learning Experience: version, IDE selection and coding Solutions
  5. Analysis of Python GIL and thread security

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.