In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of "how to deal with documents in python". The editor shows you the operation process through actual cases. The method of operation is simple and fast, and it is practical. I hope this article "how to deal with documents in python" can help you solve the problem.
1. Open () method
The python open () method is used to open a file and return a file object, which is needed during the processing of the file, and throws an OSError if the file cannot be opened.
Note: when using the open () method, the contents of the file will not be saved until the file object is closed. To close the file, you need to call the close () method.
The common form of the open () method is to receive two parameters: the file name (file) and the pattern (mode)
Basic syntax:
Open (file,mode='r')
Complete syntax:
Open (file,mode='r',buffering=1,encoding=None,errors=None,newline=None,closefd=True)
Parameter description:
File: yes, it means the file name under a certain path (either relative or absolute)
Mode: optional parameter, file opening mode
Buffering: setting buffering
Encoding: encoding, usually using utf8
Errors: error level
Newline: distinguishing newline characters
Closefd: the type of file parameter passed in
Mode stands for file opening mode. How many modes are there? References are as follows:
R: open the file as read-only. The pointer to the file will be placed at the beginning of the file. This is the default open mode for files
W: opening a file can only be used for writing. If the file exists, open the file and edit it from scratch, and the original contents of the file will be emptied. If the file does not exist, a new file is created
A: open a file to append content to it. If the file already exists, the file pointer will be placed at the end of the file. That is, after the new content will be written as the existing content. If the file does not exist, create a new file to write
Ringing: open a file for reading and writing, and the file pointer will be placed at the beginning of the file
Open a file for reading and writing. If the file already exists, the file pointer will be placed at the end of the file. When the file is opened, the append mode will be used. If the file does not exist, create a new file for reading and writing.
Open a file for reading and writing. If the file already exists, open the existing file and edit it from scratch, that is, the original content will be deleted. If the file does not exist, create a new file
B: binary file
Rb: open a file in binary format for read-only use. The file pointer will be placed at the beginning of the file. This is the default mode and is generally used for non-text files, such as pictures, videos, etc.
Wb: open a file in binary format for writing only. If the file already exists, open the file and edit it from the beginning of the file, that is, the original content will be deleted, and if the file does not exist, create a new file. Generally used for non-text files, such as pictures, videos, etc.
Ab: open a file in binary format for appending. If the file already exists, the file pointer will be placed at the end of the file, that is, the new content will be written after the existing content, and if the file does not exist, a new file will be created and written to
Ab+: opens a file in binary format for append. If the file already exists, the file pointer will be placed at the end of the file. If the file does not exist, create a new file for reading and writing.
Example 1: open a file in w mode
F = open ('myfile.txt',' w') f.write ('hellodepartment worldview') f.close () # # the output is a myfile.txt file under the current path, and write 'hello world' to the file myfile.txt as follows: hello,world!
Example 2: open a file in a mode
F=open ('myfile.txt','a') f.write (' good lucky') f.close () # # output result: the content will be appended at the end of the file, and the original content will not be overwritten. The content of myfile.txt is as follows: HelloPowerWorldworld goodwill lucky!
Example 3: if you open the file in w mode, the original content will be overwritten.
F = open ('myfile.txt',' w') f.write ('thanks') f.close () # # output: myfile.txt content is as follows: welcome!
Example 4: read files in r mode
F = open ('myfile.txt', 'r') # read the file in r mode, and then write will error f.write (' hellograms') f.close () # # output: f.write ('hellograms') io.UnsupportedOperation: not writable
Example 5: read files in r + mode
F = open ('myfile.txt',' ringing') f.write ('hellogged') f.close () # # output result: open a file for reading and writing, and the file pointer will be placed at the beginning of the file myfile.txt content: 1-> here represents the blank line 2hello!
Example 6: write files in w+ mode
F = open ('myfile.txt', 'walled') f.write ('loveable') f.close () # # output result: if the file already exists, open the existing file and edit it from scratch, that is, the original content will be deleted. If the file does not exist, a new file is created. Myfile.txt content: love! 2. Read () method
Read everything in the file, and then move the cursor to the end of the file. You must be in r or r + mode before you can use read ().
Example 7: write the file in w + mode, and then read the contents of the file in r + mode
F = open ('myfile.txt',' wicked') f.write ('HelloGramsWorldWorldGoodwill gooddieLuckyexamples') f.close () # # output result: W + if the file already exists, open the existing file and edit it from scratch, that is, the original content will be deleted. If the file does not exist, a new file is created. Myfile.txt content: Heliophony goodmum luckygift gift f = open ('myfile.txt', 'ritual') # print (f.read ()) # # output result: hellopagery Worldworld goodmum luckygift!
Example 8: write the file in r + mode, and then read the file
F = open ('myfile.txt',' ritual') # in r + mode, the file pointer will be placed at the beginning, and then the specified character will be replaced by the original character f.write ('welcom') print (f.read ()) # # output result: rldflowers goodluckycharacters! # myfile.txt content is as follows: 1-> blank line 2blank rldwords 3goodmediumluckycharacters! 3. Readlines () method
Readlines () reads the file line by line
Example 9:
F = open ('myfile.txt', 'ringing') print (f.readline ()) # # output result welcomrld! Good,lucky!!
Readlines ()
Read the contents of the file line by line and store them in the list. You can read the contents of all lines. The contents of each line are stored in the list as an element in the list, and a list is returned. This list can be processed using the for..in structure. If the EOF Terminator is encountered, a null character is returned.
Example 10:
F = open ('myfile.txt', 'r') print (f.readlines ()) # # output content: ['', 'thanks rldflowers,' goodwill luckycards'] F = open ('myfile.txt', 'r') for i in f.readlines (): I = i.strip () # remove spaces, such as the newline character print (I) # # output content:-> blank line "rld goodmum Luckyweights! 4. Seek () method
Seek () is used to move the file read pointer to the specified location
The syntax is as follows: the offset of f.seek (offset, [, whence]) offset--, that is, the number of bytes that need to be moved. If it is negative, it means that the optional parameter whence-- starts from the last bit. The default is 0. Define a parameter for offset to indicate where to start the offset; 0 means to count from the beginning of the file; 1 means to count from the current location; and 2 means to count from the end of the file. If the operation is successful, return the new file location; if the operation fails, return-1
Example 11:
F = open ('workfile.txt',' wb+') print (f.write (bread0123456789abcde')) f.seek (5) print (f.read (1)) f.seek (- 3 print (1)) print (f.read (1)) # # output result: 15bpm 5bcm c' workfile.txt content is as follows: 0123456789abcde
Example 12:
F = open ('myfile.txt', 'r') print (' filename is:', f.name) line=f.readline (). Strip () # # remove print ('the data read for the first time is% s'% (line)) f.seek (0,0) # # the first zero means the offset is 0 The second 0 means to offset line=f.readline () .strip () print from the header ('the data read the second time is% s'% (line)) # # output: the data read by filename is: myfile.txt for the first time is goodluckyread! The data read for the second time is gooddiary luckyworthy! The content of myfile.txt is as follows: gooddir luckydestroy f.seek (0,0), and the running result is as follows: F = open ('myfile.txt', 'r') print (' filename is:' F.name) line=f.readline (). Strip () print ('the first read data is% s'% (line)) line=f.readline (). Strip () print ('the second read data is% s'% (line)) # # the running result is as follows: filename is: the data read by myfile.txt for the first time is Goodwin Luckyhands! The data read for the second time is the tell () function
Returns the current location of the file
F = open ('myfile.txt',' rations') print ('filename is:', f.name) line=f.readline () print ('read data is% s'% (line)) line1=f.readline () print ('read data is% s'% (line1)) pos=f.tell () print ('current position is% Dprint'% (pos)) f.close () # # output result filename is: the data read by myfile.txt is Goodwill Luckyhands! The data read is current position is 12: the content of myfile.txt is as follows: gooddir Luckyread! # # output result filename is: the data read by myfile.txt is Goodbye Luckykeeper! The data read is gcurrent position is 15: myfile.txt content is as follows: GoodthingLuckymanipulationg on "how to file processing python" content is introduced here, thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.