当前位置:网站首页>Txt plain text operation
Txt plain text operation
2022-07-29 08:23:00 【Make Jun Huan】
TXT Plain text operation
List of articles
Preface
Save data to TXT The operation of text is very simple , and TXT Text is compatible with almost any platform , But there is a drawback , That is not conducive to retrieval .
One 、 operation TXT Ideas of the text
utilize Python Provided open Method to open a text file , Get a file operation object , Here it is assigned as file, Each part of the information extracted , using file Object's write Method writes the extracted content to the file .
After all extraction , Last call close Method to turn it off , The content can be successfully written into the text .
In fact, that is open、write、close The usage of these three methods .
open The first parameter of the method is the name of the target file to be saved ;
The second parameter is w, Represents writing text by overwriting ;
in addition , We also specified the file code as utf-8.
Last , After writing , You also need to call close Method to close the file object .
Two 、 Open mode ----open
open The second parameter of the method is set to w, This will empty the source file every time text is written , Then write the new content to the file , This is a file opening method . About how to open the file , In fact, there are several other , Here is a brief introduction .
| Parameters | effect |
|---|---|
| r | Open the file read-only , It means that you can only read the contents of the file , Cannot write file contents . This is the default mode . |
| rb | Open a file in binary read-only mode , Usually used to open binary files , Like audio 、 picture 、 Video and so on . |
| r+ | Open a file read-write , You can read and write files . |
| rb+ | Open a file in binary read-write mode , You can also read and write , But what is read and written is binary data . |
| w | Open a file by writing . If the file already exists , Then cover it . If the file does not exist , Create a new file . |
| wb | Open a file by binary writing . If the file already exists , Then cover it . If the file does not exist , Create a new file . |
| w+ | Open a file read-write . If the file already exists , Then cover it . If the file does not exist , Create a new file . |
| wb+ | Open a file in binary read-write format . If the file already exists , Then cover it . If the file does not exist , Create a new file . |
| a | Open a file in append mode . If the file already exists , The file pointer will be placed at the end of the file . in other words , The new content will be written after the existing content . If the file does not exist , Create a new file to write . |
| ab | Open a file by binary append . If the file already exists , Then the file pointer will be placed at the end of the file . in other words , The new content will be written after the existing content . If the file does not exist , Create a new file to write . |
| a+ | Open a file read-write . If the file already exists , The file pointer will be placed at the end of the file . Append mode when the file opens . If the file does not exist , Create a new file to read and write . |
| ab+ | Open a file by binary append . If the file already exists , Then 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 . |
2. Code
In the use of with as grammar . stay with At the end of the control block , The file will close automatically , So there is no need to call close The method .
with open('game_data.txt', 'a',encoding='utf-8') as txtfile:
txtfile.write(f' Content to write text ')
summary
This article only briefly introduces open Use
边栏推荐
- Low cost 2.4GHz wireless transceiver chip -- ci24r1
- Eps32+platform+arduino running lantern
- Segment paging and segment page combination
- STM32 serial port garbled
- Arduino uno error analysis avrdude: stk500_ recv(): programmer is not responding
- Application of explosion-proof inclination sensor in safe operation of LNG
- 【OpenCV】-算子(Sobel、Canny、Laplacian)学习
- Detailed steps of installing MySQL 5.7 for windows
- Dp1332e multi protocol highly integrated contactless read-write chip
- Simulation of four way responder based on 51 single chip microcomputer
猜你喜欢
![[academic related] why can't many domestic scholars' AI papers be reproduced?](/img/1a/7b162741aa7ef09538355001bf45e7.png)
[academic related] why can't many domestic scholars' AI papers be reproduced?

DAC0832 waveform generator based on 51 single chip microcomputer

Second week of postgraduate freshman training: convolutional neural network foundation

Simplefoc+platformio stepping on the path of the pit

Week 2: convolutional neural network basics
![[beauty of software engineering - column notes] 29 | automated testing: how to kill bugs in the cradle?](/img/e1/8a61f85bf93801d842e78ab4f7edc7.png)
[beauty of software engineering - column notes] 29 | automated testing: how to kill bugs in the cradle?

分段分页以及段页结合
![[beauty of software engineering - column notes] 25 | what methods can improve development efficiency?](/img/c8/c2d45abbf36b898040f9f1cf6274ff.png)
[beauty of software engineering - column notes] 25 | what methods can improve development efficiency?

TCP——滑动窗口
![[robomaster] a board receives jy-me01 angle sensor data -- Modbus Protocol & CRC software verification](/img/0e/e5be0fffb154d081c20b09832530d4.png)
[robomaster] a board receives jy-me01 angle sensor data -- Modbus Protocol & CRC software verification
随机推荐
Detailed steps of installing MySQL 5.7 for windows
Stm8s003 domestic substitute for dp32g003 32-bit microcontroller chip
分段分页以及段页结合
Eps32+platform+arduino running lantern
ROS tutorial (Xavier)
Simplefoc+platformio stepping on the path of the pit
[beauty of software engineering - column notes] 24 | technical debt: continue to make do with it, or overthrow it and start over?
Phy6252 is an ultra-low power Bluetooth wireless communication chip for the Internet of things
Reading papers on false news detection (4): a novel self-learning semi supervised deep learning network to detect fake news on
Alibaba political commissar system - Chapter 1: political commissars are built on companies
Segment paging and segment page combination
STM32 serial port garbled
Simulation of four way responder based on 51 single chip microcomputer
Hal library learning notes - 8 concept of serial communication
Ws2812b color lamp driver based on f407zgt6
Third week weekly report resnet+resnext
Data warehouse layered design and data synchronization,, 220728,,,,
Low power Bluetooth 5.0 chip nrf52832-qfaa
[beauty of software engineering - column notes] 26 | continuous delivery: how to release new versions to the production environment at any time?
Unity多人联机框架Mirro学习记录(一)