Python learning the next day (File Processing ),

Source: Internet
Author: User

Python learning the next day (File Processing ),

The learning content is:

  • Python File Processing
  • List, tuples, and dictionary usage
  • Use of Collections
  • Function

File. textd content

A person with high EQ doesn't often criticize people.That's because criticizing usually doesn't solve any problem.Everybody wants to solve problems. It's just that many people don't know how. And criticizing doesn't change this.

 

Code for reading the file.txt File

F = file ("file.txt", 'R') # print f. readline () line_sz = [] for line in f. readlines (): print line = line. strip ("\ n") # Remove the linefeed line_sz.append (line) # Write the content of each line to the array f. close () # close the print line_sz File

Through the above code, I can understandPython File Reading ProcessOpen the file with file (f. file ("file.txt", 'R'), read all the content of the file (f. readlines), and traverse each row cyclically.

1. File processing mode

  • R is enabled in read-only mode.
  • W is enabled in write-only mode.
  • A. open the file in append mode.
  • R + B is enabled in read/write mode
  • W + B Open in write-Read mode
  • A + B is enabled in append and read mode.

 

Summary and extension

Example: d = "h: I: d: c: g: k"

Print d. split (":") --> [h, I, d, c, g, k]

2. strip () can delete the final leading character of the string

Example: d = "h: I: d: c: g: k :"

Print d. split (":") --> h: d: c: g: k

 

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.