Python basic tutorial notes-Project 1-instant tag-Day1

Source: Internet
Author: User

Python basic tutorial notes-Project 1-instant tag-Day1

Objective: to convert the Upload File (.txt into a web page (.html)

Test document test_input.txt:

Welcome to World Wide Spam, Inc.These are the corporate web pages of *World Wide Spam*, Inc. We hopeyou find your stay enjoyable, and that you will sample many of ourproducts.A short history of the companyWorld Wide Spam was started in the summer of 2000. The businessconcept was to ride the dot-com wave and to make money both throughbulk email and by selling canned meat online.After receiving several complaints from customers who weren'tsatisfied by their bulk email, World Wide Spam altered their profile,and focused 100% on canned goods. Today, they rank as the world's13,892nd online supplier of SPAM.DestinationsFrom this page you may visit several of our interesting web pages:  - What is SPAM? (http://wwspam.fu/whatisspam)  - How do they make it? (http://wwspam.fu/howtomakeit)  - Why should I eat it? (http://wwspam.fu/whyeatit)How to get in touch with usYou can get in touch with us in *many* ways: By phone (555-1234), byemail (wwspam@wwspam.fu) or by visiting our customer feedback page(http://wwspam.fu/feedback).
Simple_markup.py:



First, split the text into blocks:

Collect all the rows that are encountered until an empty row is encountered, and then return the collected rows. The returned rows are a block. And then start collecting again. Do not collect empty rows or return empty blocks.

At the same time, make sure that the last line of the file is empty, otherwise the program will not know when the last block will end. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + zsSxvr/fuse + CjxwcmUgY2xhc3M9 "brush: java;"> def lines (file): for line in file: yield line yield '\ n' def blocks (file ): block = [] for line in lines (file): if line. strip (): block. append (line) elif block: yield ''. join (block ). strip () block = []The lines generator only adds an empty row blocks generator to the end of the file to implement the method described above.

Concept: generator)

What is a generator?

First look at a piece of JAVA code:

import java.util.ArrayList;import java.util.List;public class test {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubList
 
   list = new ArrayList
  
   ();for (int i = 0; i < 10; ++i)list.add("Num:" + i);for (String str : list)System.out.println(str);}}
  
 
The code function inserts and prints the string "Num0" to "Num9" in the List. Execution result:

The generator also has this effect. Now let's look at the python code:

def func():yield 0yield 1yield 2for i in func():print i
Execution result:



In addition, the next () method (similar to the JAVA iterator?) is available for the generator on the Internet ?) :

def func():yield 0yield 1yield 2f = func()print f.next()print f.next()for i in func():print i
Execution result:



The following uses a generator to print the Fibonacci sequence below 300:

def get_fib():a = b = 1yield ayield bwhile a + b < 300:a,b = b,a+byield bfor num in get_fib():print num
Execution result:



For the moment, it seems that the generator is such a thing.

Now let's look at the code util. py:

1. First, let's take a look at the generator lines (file ):

def lines(file):    for line in file: yield line    yield '\n'
Test:

a = [1,2,3,4,5]b = ['hi','how are you','how do you do']def lines(arg):for line in arg:yield lineyield '\n'for i in lines(a):print ifor j in lines(b):print j
Execution result:


The line generator is used to append an empty line at the end of the file.


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.