Python's text data

Source: Internet
Author: User

Some methods of string!

1.text.endswith (". jpg") returns the value TRUE if the string is terminated with the given substring.

2. Text.upper (): Returns a copy of a string that is converted to all uppercase letters.

3.text.lower (): Returns a copy of a string that is converted to a full lowercase letter.

4.text.replace ("Tomorrow", "Tuesday"): Returns a copy of a string in which a substring is completely replaced with another substring.

5.text.strip (): Returns a copy of the string that removes the opening and trailing spaces.

6. Text.find ("Python"): Returns the first character index value of a substring when a given substring is found.

7.text.startswith ("<HTML>") returns True if the string starts with a given substring.

8.TEXT[A,B] takes a substring of the string that begins with the a index value to the end of the B index value but does not include the character corresponding to the B index value.

Import Urllib.requestpage=urllib.request.urlopen ("http://www.baidu.com") Text=page.read (). Decode ("UTF8") print ( Text

  

After adding a method to take out the substring

Import Urllib.requestpage=urllib.request.urlopen ("http://www.baidu.com") Text=page.read (). Decode ("UTF8") price= Text[234:238]print (Price)

  

where "#293的索引值分别为: 234,235,236,237,238 But the result of the above code is:

Remove substring after a given substring

Import Urllib.requestpage=urllib.request.urlopen ("http://www.baidu.com") Text=page.read (). Decode ("UTF8") where= Text.find (' = "') start_of_price=where+2end_of_price=start_of_price+4price=text[start_of_price:end_of_price]print (Price)

  

The type converted to float after the specific character is removed is compared to 4.47 if it is less than 4.47 and outputs: buy!

Import urllib.requestprice=99.99while price>4.47:    page=urllib.request.urlopen ("http://www.baidu.com")    Text=page.read (). Decode ("UTF8")    where=text.find (' = "')    start_of_price=where+2    end_of_price= Start_of_price+4    price=float (Text[start_of_price:end_of_price]) print ("buy!")

Python's built-in time library

Python Library Documentation: Time

Time.clock () represents the current time in seconds, using the floating-point number format.

Time.daylight () If you are not currently in daylight saving time, return 0.

Time.gmtime () gives the current date and time of the UTC time (not affected by your time zone!). )。

Time.localtime () gives the current local time (this is affected by your time zone).

Time.sleep (secs) rests within a given number of seconds, and does nothing.

Time.time () gives the number of seconds from January 1, 1970 to the current count.

Time.timezone () gives the number of hours between your time zone and the UTC (London) time zone.

Import Urllib.requestimport timeprice=99.99while price>4.47:    time.sleep (page=)    Urllib.request.urlopen ("http://www.baidu.com")    Text=page.read (). Decode ("UTF8")    where=text.find (' = ')    start_of_price=where+2    end_of_price=start_of_price+4    price=float (text[start_of_price:end_of_price ]) print ("huy!")

  

Summarize

s represents a string.

S[4] Gets the 5th character in the S string.

S[6:12] Gets a substring in the string s. (Until the index value is 12 characters, but not included!)

S.find () is used to search for strings.

S.upper () Converts the string to all uppercase letters.

Float () Converts a string to a number with a decimal fraction, which is a floating-point.

+ "Addition" operator

> "Greater than" operator

Urllib. The request library is used to communicate with the Web

The time library is used for work related to dates/times!

Python's text data

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.