I. Opening and creation of files
>>> f = open ('/tmp/test.txt ') >>> f.read () ' Hello Python!\nhello world!\n ' >>> f
Second, the file read
Step: Open-read-close
>>> f = open ('/tmp/test.txt ') >>> f.read () ' Hello Python!\nhello world!\n ' >>> f.close ()
Reading data is a necessary step for late data processing.. txt is a widely used data file format. Some. csv,. xlsx and other files can be converted to. txt files for reading. I often use Python's I/O interface, read the data into the list, and then use the NumPy scientific calculation package to convert the list data into the array format, so that it can be used as a MATLAB scientific calculation.
The following is a common read TXT file code, can be used in most of the TXT file read
filename = ' array_reflection_2d_tm_vertical_norme_center.txt ' # TXT file and current script are in the same directory, so do not write the specific path pos = []efield = []with open ( FileName, ' R ') as File_to_read: While True: lines = File_to_read.readline () # full row read data if not lines: break Pass p_tmp, e_tmp = [Float (i) for I in Lines.split ()] # splits the entire row of data, and if the delimiter is a space, the parentheses do not pass in the parameter, and if it is a comma, the ', ' character is passed in. pos.append (p_tmp) # Add newly read data efield.append (e_tmp) pass pos = Np.array (POS) # Converts data from the list type to the array type. Efield = Np.array (efield) Pass
For example, here is the TXT file that will be read in
After reading, enthought canopy's variable window looks at the read-in data, the Pos on the left, and the Efield on the right.
Third, file write (cautious, careful not to empty the original file)
Step: Open--write--(save) Close
Writing data directly is not possible because the default is ' R ' read-only mode
>>> f.write (' Hello Boy ') Traceback (most recent call last): File "
", line 1, in
Ioerror:file not open for writing>>> F
You should specify a writable mode first
>>> f1 = open ('/tmp/test.txt ', ' W ') >>> f1.write (' Hello boy! ')
But at this point the data is only written to the cache, not saved to the file, and from the following output can be seen, the original configuration was emptied
[Root@node1 ~]# Cat/tmp/test.txt[root@node1 ~]#
Close this file to write the data in the cache to a file
>>> f1.close () [Root@node1 ~]# cat/tmp/test.txt[root@node1 ~]# Hello boy!
Note: This step requires considerable caution, because if the edited file exists, this step will first empty the file and then write it again. So what if you don't empty the file and write it?
Using the r+ mode does not first empty, but replaces the original file, as in the following example: Hello boy! be replaced with Hello aay!
>>> F2 = open ('/tmp/test.txt ', ' r+ ') >>> f2.write (' \nhello aa! ') >>> f2.close () [Root@node1 python]# Cat/tmp/test.txthello aay!
How do I implement non-replacement?
>>> F2 = open ('/tmp/test.txt ', ' r+ ') >>> f2.read () ' Hello girl! ' >>> f2.write (' \nhello boy! ') >>> f2.close () [Root@node1 python]# Cat/tmp/test.txthello Girl!hello boy!
As you can see, if you read the file before writing and then write it, the data you write is added to the end of the file without replacing the original file. This is due to the pointer, the r+ mode pointer is the default at the beginning of the file, if written directly, it will overwrite the source file, read () After reading the file, the pointer will move to the end of the file, then write the data there is no problem. A mode can also be used here
>>> f = open ('/tmp/test.txt ', ' a ') >>> f.write (' \nhello man! ') >>> f.close () >>>[root@node1 python]# Cat/tmp/test.txthello Girl!hello Boy!hello man!
For an introduction to other models, see the following table:
Methods for file objects:
F.readline () reading data row by line
Method One:
>>> f = open ('/tmp/test.txt ') >>> f.readline () ' Hello girl!\n ' >>> f.readline () ' Hello boy!\n ' >>> f.readline () ' Hello man! ' >>> F.readline () "
Method Two:
>>> for I in open ('/tmp/test.txt '): ... Print I...hello Girl!hello Boy!hello man!f.readlines () Store the contents of the file as a list >>> f = open ('/tmp/test.txt ') > >> f.readlines () [' Hello girl!\n ', ' Hello boy!\n ', ' Hello man! '] >>> F.close ()
F.next () reads data row by line, similar to F.readline (), except that f.readline () reads to the end if no data is returned empty and f.next () does not read the data.
>>> f = open ('/tmp/test.txt ') >>> f.readlines () [' Hello girl!\n ', ' Hello boy!\n ', ' Hello man! '] >>> f.close () >>>>>> f = open ('/tmp/test.txt ') >>> f.next () ' Hello girl!\n ' >> > f.next () ' Hello boy!\n ' >>> f.next () ' Hello man! ' >>> F.next () Traceback (most recent): File "
", line 1, in
stopiteration
F.writelines () Multi-line write
>>> L = [' \nhello dear! ', ' \nhello son! ', ' \nhello baby!\n ']>>> f = open ('/tmp/test.txt ', ' a ') >> > F.writelines (L) >>> f.close () [Root@node1 python]# Cat/tmp/test.txthello Girl!hello Boy!hello Man!hello Dear!hello Son!hello baby!
F.seek (offset, option)
>>> f = open ('/tmp/test.txt ', ' r+ ') >>> f.readline () ' Hello girl!\n ' >>> f.readline () ' Hello boy!\n ' >>> f.readline () ' Hello man!\n ' >>> f.readline () ' >>> f.close () >>> f = open ( '/tmp/test.txt ', ' r+ ') >>> f.read () ' Hello Girl!\nhello boy!\nhello man!\n ' >>> f.readline () ' > >> F.close ()
This example can fully explain the previous use of r+ this mode, why need to perform f.read () before normal insertion
F.seek (offset, option)
(1) option = 0, which indicates that the file pointer is pointing from the file header to the "offset" byte
(2) option = 1, which means pointing the file pointer from the current position of the file, and moving the "offset" byte backwards
(3) option = 2, which means pointing the file pointer from the end of the file, moving the "offset" byte forward
Offset: Positive number means offset to the right, negative to left
>>> f = open ('/tmp/test.txt ', ' r+ ') >>> F.seek (0,2) >>> f.readline () ' >>> F.seek ( 0,0) >>> f.readline () ' Hello girl!\n ' >>> f.readline () ' Hello boy!\n ' >>> f.readline () ' Hello man!\n ' >>> f.readline () '
F.flush () writes modifications to the file (without closing the file)
>>> f.write (' Hello python! ') >>> F.flush ()
[Root@node1 python]# Cat/tmp/test.txt
Hello Girl!hello boy!hello Man!hello python!
F.tell () Get pointer position
>>> f = open ('/tmp/test.txt ') >>> f.readline () ' Hello girl!\n ' >>> f.tell () 12>>> F.readline () ' Hello boy!\n ' >>> F.tell () 23
Iv. Content discovery and replacement
1. Content Search
Example: Number of Hello in statistics file
Idea: Open file, traverse file content, match keyword by regular expression, count match number.
[Root@node1 ~]# Cat/tmp/test.txt
Hello Girl!hello boy!hello Man!hello python!
The script is as follows:
Method One:
#!/usr/bin/pythonimport ref = open ('/tmp/test.txt ') Source = F.read () f.close () R = R ' Hello ' s = Len (Re.findall (R,source)) Print S[root@node1 python]# python count.py4
Method Two:
#!/usr/bin/pythonimport REFP = File ("/tmp/test.txt", ' r ') Count = 0for s in fp.readlines (): Li = Re.findall ("Hello", s) if Len (li) >0:count = count + len (LI) print "Search", Count, "Hello" fp.close () [Root@node1 python]# python count1.pysearch 4 hel Lo
2. Replace
Example: Change the test.txt in the hello to "HI" and save the result in Myhello.txt.
#!/usr/bin/pythonimport ref1 = open ('/tmp/test.txt ') F2 = open ('/tmp/myhello.txt ', ' r+ ') for S in F1.readlines (): F2.write (S.replace (' Hello ', ' Hi ')) f1.close () f2.close () [Root@node1 python]# touch/tmp/myhello.txt[root@node1 ~]# cat/tmp/ Myhello.txthi Girl!hi Boy!hi Man!hi python!
Instance: reads the file test.txt content, strips empty lines and comment lines, sorts them in the unit of behavior, and outputs the results as result.txt. The contents of the Test.txt are as follows:
The script is as follows:
f = open (' cdays-4-test.txt ') result = list () for lines in F.readlines (): # Read Data line = Line.strip () #去掉每行头尾空白if not Len (line) or Line.startswith (' # '): # To determine if it is a blank row or comment line continue #是的话, skip processing result.append (lines) #保存result. Sort () #排序结果print resultopen (' Cdays-4-result.txt ', ' W '). Write ('%s '% ' \ n '. Join (Result)) #保存入结果文件