This article mainly introduces how to read and write txt text files in Python, including explanation of text search and replacement techniques. For more information, see
I. opening and creating a file
>>> f = open('/tmp/test.txt')>>> f.read()'hello python!\nhello world!\n'>>> f
II. file reading
Step: Enable -- read -- disable
>>> f = open('/tmp/test.txt')>>> f.read()'hello python!\nhello world!\n'>>> f.close()
Reading data is a necessary step for later data processing .. Txt is a widely used data file format. A few. csv and. xlsxfiles can be converted to. txt files for reading. I often use Python's built-in I/O interface to read data and store it in the list. then, I use the numpy scientific computation package to convert the list data to the array format, so that scientific computation can be performed like MATLAB.
Below is a commonly used code for reading txt files, which can be used in most txt files.
Filename = 'array_reflection_2D_TM_vertical_normE_center.txt '# The txt file is in the same directory as the current script, so you do not need to write the path pos = [] Efield = [] with open (filename, 'r') as file: while True: lines = file_to_read.readline () # The whole row reads data if not lines: break pass p_tmp, E_tmp = [float (I) for I in lines. split ()] # split the entire line of data. if the delimiter is a space, no parameter is required in the brackets. if the delimiter is a comma, the ',' character is input. Pos. append (p_tmp) # add the newly read data Efield. append (E_tmp) pass pos = np. array (pos) # convert the data from list type to array type. Efield = np. array (Efield) pass
For example, the following is the txt file to be read.
After reading, you can view the read data in the variable window of Enthought Canopy. the left side is pos and the right side is Efield.
3. file writing (be careful not to clear the original file)
Step: Open -- write -- (save) Close
Writing data directly is not acceptable because the 'R' read-only mode is enabled by default.
>>> f.write('hello boy')Traceback (most recent call last):File "
", line 1, in
IOError: File not open for writing>>> f
Specify the writable mode first.
>>> f1 = open('/tmp/test.txt','w')>>> f1.write('hello boy!')
However, the data is only written to the cache and not saved to the file. the output below shows that the configuration in the original file is cleared.
[root@node1 ~]# cat /tmp/test.txt[root@node1 ~]#
Close this file to write the cached data to the file.
>>> f1.close()[root@node1 ~]# cat /tmp/test.txt[root@node1 ~]# hello boy!
Note: This step requires caution, because if the edited file exists, the file will be cleared and then written again. What should I do if I don't want to clear the file and write it again?
The r + mode will not be cleared first, but will replace the original file, as shown in the following example: hello boy! 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 can I achieve replacement without 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, the written data will be added to the end of the file without replacing the original file. This is caused by pointers. the pointer in r + mode is at the beginning of the file by default. if it is written directly, the source file will be overwritten. after reading the file through read, the pointer will be moved to the end of the file, and data writing will not be faulty. The 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!
The following table describes other modes:
File object method:
F. readline () reads data row by row
Method 1:
>>> f = open('/tmp/test.txt')>>> f.readline()'hello girl!\n'>>> f.readline()'hello boy!\n'>>> f.readline()'hello man!'>>> f.readline()''
Method 2:
>>> For I in open ('/tmp/test.txt'):... print I... hello girl! Hello boy! Hello man! F. readlines () stores the file content in the form of 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 row, and f. readline () is similar. The only difference is that f. when readline () is read to the end, if there is no data, it will return null, and f. next () if no data is read, an error is returned.
>>> 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 call last):File "
", line 1, in
StopIteration
F. writelines () multi-row 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 why f. read () needs to be executed before the r + mode can be inserted normally.
F. seek (offset, option)
(1) option = 0, indicating to point the file pointer to the "offset" byte from the file header
(2) option = 1, which indicates pointing the file pointer to the current position of the file, and moving the "offset" byte backward
(3) option = 2, indicating to point the file pointer to the end of the file and move the "offset" byte forward.
Offset: positive value indicates the offset to the right, and negative value indicates the offset to the 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 () write the changes to the file (no need to close 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 search and replacement
1. content search
Instance: Number of hello messages in the statistics file
Idea: open a file, traverse the file content, use regular expressions to match keywords, and count the number of matches.
[root@node1 ~]# cat /tmp/test.txt
hello girl!hello boy!hello man!hello python!
The script is as follows:
Method 1:
#!/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 2:
#!/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 hello
2. replacement
Instance: replace all the hello messages in test.txt with "hi", and save the result to 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!
Example: read the file test.txtcontent and sort the output result into result.txt. The content of test.txt is as follows:
#some wordsSometimes in life,You find a special friend;Someone who changes your life just by being part of it.Someone who makes you laugh until you can't stop;Someone who makes you believe that there really is good in the world.Someone who convinces you that there really is an unlocked door just waiting for you to open it.This is Forever Friendship.when you're down,and the world seems dark and empty,Your forever friend lifts you up in spirits and makes that dark and empty worldsuddenly seem bright and full.Your forever friend gets you through the hard times,the sad times,and the confused times.If you turn and walk away,Your forever friend follows,If you lose you way,Your forever friend guides you and cheers you on.Your forever friend holds your hand and tells you that everything is going to be okay.
The script is as follows:
F = open('cdays-4-test.txt ') result = list () for line in f. readlines (): # read data line by line = line. strip () # remove the blank spaces at the beginning and end of each line if not len (line) or line. startswith ('#'): # if the row is null or the comment row continue # is yes, skip not to process the result. append (line) # Save result. sort () # print resultopen('cdays-4-result.txt ', 'w '). write ('% s' %' \ n '. join (result) # save the input result file