当前位置:网站首页>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
边栏推荐
- STM32 serial port garbled
- HC-SR04超声波测距模块使用方法和例程(STM32)
- Dp4301-sub-1g highly integrated wireless transceiver chip
- Proteus simulation based on msp430f2491
- Ws2812b color lamp driver based on f407zgt6
- Random lottery turntable wechat applet project source code
- What constitutes the smart charging pile system?
- New energy shared charging pile management and operation platform
- Tb6600+stm32f407 test
- NFC two-way communication 13.56MHz contactless reader chip -- si512 replaces pn512
猜你喜欢

Alibaba political commissar system - Chapter 1: political commissars are built on companies

Simplefoc parameter adjustment 3-pid parameter setting strategy

Unity shader learning (VI) achieving radar scanning effect

Inclination sensor accuracy calibration test

STM32 serial port garbled

A problem encountered in SQL interview

数仓分层设计及数据同步问题,,220728,,,,

Noise monitoring and sensing system

Clion+opencv+aruco+cmake configuration

随机抽奖转盘微信小程序项目源码
随机推荐
Simplefoc+platformio stepping on the path of the pit
Inclination sensor accuracy calibration test
Day4: the establishment of MySQL database and its simplicity and practicality
Proteus simulation based on msp430f2491
Stm32ff030 replaces domestic MCU dp32g030
阿里巴巴政委体系-第一章、政委建在连队上
AES bidirectional encryption and decryption tool
Day4: SQL server is easy to use
DAC0832 waveform generator based on 51 single chip microcomputer
Windows 安装 MySQL 5.7详细步骤
Qt/pyqt window type and window flag
Proteus simulation based on 51 MCU ADC0808
Stm8s003 domestic substitute for dp32g003 32-bit microcontroller chip
深度学习(2):图片文字识别
PostgreSQL手动创建HikariDataSource解决报错Cannot commit when autoCommit is enabled
DC motor control system based on DAC0832
谷歌浏览器免跨域配置
SQL 面试碰到的一个问题
Reading papers on false news detection (4): a novel self-learning semi supervised deep learning network to detect fake news on
2.4G band wireless transceiver chip si24r1 summary answer