Python files and data format (Tutorial)

File is a sequence of data stored in the sub main memory, the data may comprise any content.

Conceptually, the data file is a collection of abstract and similar function is set and the program abstract.

Document the organization and expression data more efficient and more flexible.

File two forms, text files and binary files.

Text file: General by a single specific encoding of characters

Binaries: general by the bits 0 and 1, with no Unicode.

Whether through text file or binary created, can be opened in two ways.

 Let's give an example compare text files and binary files,

First we create a text file on the desktop, named example, and write "I love my country."

As follows:

 

 Note: the lower right corner of encoding as "UTF-8"

And then execute the code:

fp=open("example.txt","rt",encoding='UTF-8')
print(fp.readline())
fp.close()

fps=open("example.txt","rb")
print(fps.readline())
fps.close()

 

In this code, respectively, using text files and binary forms, open the document, then read off.

Note: rt as a text file, rb binary file format.

Run results are shown below:

 

 

Next, we learn the official documents related operations.

1. The opening and closing files

When it comes to opening and closing files, I am reminded of the joke before, the elephant put into the refrigerator a few steps?

File operations and this joke, first open the file, and then follow, and finally close the file.

Open the file:

The default file is stored in the state, only to turn the current program have the right to operate this file.

Open the file does not exist, the program will create a new file to operate.

After opening the file, the file is in an occupied state, another process can not operate on the document.

Open file required open () function, the following format:

<Variable name> = open (<file name>, <open mode>)

encoding for the encoding

File Open Mode:

"R": read-only mode, the file does not exist abnormal return

"W": overwrite mode, the file does not exist, create the file exists completely covered

"X": Create a write mode, the file does not exist is created, there is an abnormal returns

"A": additional write mode, the file does not exist, create the file exists in the steady last additional content

"B": binary mode

"F": text files (default)

"+": And r w \ x \ a used together, \ added based on the original read and write functions simultaneously

Open mode is a character string, wherein r, w, x, a and can b, t, + combination

As just an example of the Open: rt, rb

encoding for the encoding

 

Close the file:

After the files, to use close (function closes the file, the file's release license)

Use as follows:

<Variable name> .close ()

 

2. read the file

After the file is opened, according to the different ways may be open the corresponding file read and write operations. (Remember to pay attention to coding)

Document reading method is as follows:

<file>.read(size=-1)

Read from the file the entire file contents, given the length of the parameter string is read before the size

<file>.readline(size=-1)

A line read from the file content, is given if the parameter string of each line is read length size

<file>.readlines(hint=-1)

From the file reads the entire file contents to a list of each act, if to participate in, only read the first hint row

 

举个例子,还是刚刚的example文件,我们多输入两行同样的文件,逐行读出。

先打开文件,然后for循环,通过readlines()逐行读取、最后关闭。

代码如下:

fp=open("example.txt","rt",encoding='UTF-8')
for line in fp.readlines():
    print(line)
fp.close()

 

运行结果如下:

 

 

3.文件的写入

写入的常见函数有三个

<file>.write(s)

向文件写入一个字符串

<file>.writelines(lines)

讲一个元素全为字符串的列表写入文件

<file>.seek(offset)

改变当前文件操作指针的位置,offset的值“0”为文件开头、“1”当前位置,“2”文件结尾

 

举个例子,向文件写入一个列表类型[“中国”、“美国”、“法国”]

代码如下:

fp=open("example.txt","w+",encoding='UTF-8')
lst=["中国","美国","法国"]
fp.writelines(lst)
fp.close()

 

结果如下:

 

 

至此便是python文件方面的常见操作

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/lyy135146/p/11965603.html