Debug Log 1 (python)

Source: Internet
Author: User
Tags generator xpath

1. when writing to a file in Python, if you want to enter a carriage return each time you write, the procedure is:f.write (str + ' \ n '). \ n is to be quoted, the Unix system will write directly, the win system will be automatically replaced by \ r \ n. If you write ' \ r \ n ' Here, this is the equivalent of a carriage return plus a blank line on Windows.

2. Be aware that ' \ ' is the escape character on the Windows operating system , '/' is except,'//' is the remainder.

3. The three parameters of range () in Python are the starting element, the terminating value, and the step size. But the terminating value is not included in the. This returns a list, which is an iterative object.

4. When using the lxml Library, both Lxml.etree.parse and Lxml.etree.HTML return a ElementTree object (the node tree ). The parse method is generated from the file stream object, and theHTML is generated from the string object. the XPath method is then used for parsing. when using the parse and XPath methods, there may be garbled characters in Chinese, because encoding= ' Utf-8 ' is not set when using Htmlparser to generate the parser object.

5. In brief,The effect of yield is to turn a function into aGenerator, withThe yield function is no longer a normal function,The Python interpreter treats it as aGenerator, callingFab (5) does not performFab function instead of returning aIterable Object! In for loop execution, each loop executes the code inside the fab function returns an iterative value, and the next iteration, the code proceeds from the next statement of yield. A function with generator, which, unlike normal functions, generates a Yield statement is interrupted and an iteration value is returned, and the next execution from Yield was interrupted several times, and each interrupt would return the current iteration value by yield. Another example of yield is from file reads. If the Read () method is called directly to a file object, it causes unpredictable memory consumption. A good approach is to use fixed-length buffers to continuously read the contents of the file. With yield, we no longer need to write an iterative class of read files, so that you can easily implement file reads.

Debug Log 1 (python)

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.