Get the App
SLTechnology News&Howtos  ›  Development  › 

How to read and write Python text File and csv File

Shulou Source: shulou.com Published: 2022-06-01 05:58:30 09月17日 Update

Most people do not understand the knowledge points of this article "Python text file and csv file how to read and write", so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "Python text file and csv file how to read and write" article.

1. Reading and writing of text files 1 read () method file_object.read ([size]) of reading files

File_object represents a file object

Size indicates the length of the read data in bytes, or read to the end of the file if omitted by size

The return value is the read string

2 read the readline () method file_object.readline ([size]) of the file

Size indicates the length of the read data in bytes, or read to the end of the file if omitted by size

The return value is the read string

Read by line

3 read the readlines () method file_object.readlines ([sizehint]) of the file

Sizehint indicates the length of the read data in bytes, or read to the end of the file if omitted by size

The return value is the list of strings read, and the line in the file will be used as an element in the list

4 write () method file_object.write (str) 5 write file writelines () method file_object.writelines (sequence)

Each element in the sequence will act as a line in the file

If there is a newline character at the end of each line in the file, it is determined in the sequence element before writing

II. Reading and writing of csv files

Binary files are read the same as write and text files, as long as the open mode "b" is specified when the file is opened

1 the csv.reader () method of reading csv files

Csv.reader (file_object)

2 the csv.DictReader () method of reading csv files

Csv.DictReader (file_object)

3 csv.writer () method of writing csv file

Csv.writer (file_object)

4 csv.DictWriter () method of writing csv file

Csv.DictWriter (file_object,column_name) above is about the content of this article on "how to read and write Python text files and csv files". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.

Tags: File method text content element unit character string byte data length line sequence article knowledge article same binary value most Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Apple OPPO Reno Microsoft MariaDB