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