How to open related files in Python Module I

Source: Internet
Author: User

Python is a commonly used computer language in a wide range of applications, today, we will introduce how to operate related files in common Python Module I. The following is an introduction to this article. I hope you will gain some benefits.

Open a file

 
 
  1. f = open("filename", "r")  
R read-only w write rw read/write rb Read Binary wb write binary w + write append read/write File
 
 
 

  1. F. write ("a") f. write (str) write a string f. writeline ()
    F. readlines () is similar to the lower read
  2. F. read () all read out f. read (size) indicates reading size characters from the file
  3. F. readline () reads a row and ends with the end of the file. f. readlines () is returned ()
    Read all, return a list. list each element represents a row, contains "\ n "\
  4. F. tell () returns the current file read location
  5. F. seek (off, where) locates the file read/write position. off indicates the offset,
    A positive number moves toward the end of the file. A negative number indicates moving toward the beginning.
  6. Where 0 indicates the start time, 1 indicates the current position, and 2 indicates the end time.
  7. F. flush () refresh the cache

Close files in Python Module I

 
 
  1. f.close()  
  2. regular expression_r_r_r_r_r_r_r_r 

Regular Expression import re simple regexp

 
 
  1. p = re.compile("abc") if p.match("abc") : print "match" 

In the above example, the first character is a pattern. If it matches a string, a match object is returned. Except for some special characters, metacharacter metacter. Most of the characters match itself.
These special characters are

 
 
  1. 。^ $ * + ? { [ ] \ | ( ) 

Character Set combination (expressed in)
Listing characters. For example, [abc] indicates matching a, B, or c. Most metacharacters only indicate matching with themselves in. Example: a = ". ^ $ * +? {\\| () "Most metachar matches itself in [], but" ^ [] \ "is different

 
 
  1. p = re.compile("["+a+"]")  
  2. for i in a:  
  3. if p.match(i):  
  4. print "[%s] is match" %i  
  5. else:  
  6. print "[%s] is not match" %i 

[] Contains [], indicating "[" or "]" matching. it is represented by \ [and. ^ it appears at the beginning of [], indicating reverse. [^ abc] indicates all characters except a, B, and c. ^ It does not appear at the beginning, that is, body matching. -The range. [a-zA-Z] matches any English letter. [0-9] matches any number. \ Wonderful use in.

 
 
  1. \d [0-9]  
  2. \D [^0-9]  
  3. \s [ \t\n\r\f\v]  
  4. \S [^ \t\n\r\f\v]  
  5. \w [a-zA-Z0-9_]  
  6. \W [^a-zA-Z0-9_] 

The above article introduces file operations in Python Module I.

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.