File and data formatting

Posted by mr_tron on Thu, 16 Dec 2021 16:42:33 +0100

7.1 document overview

Document identification

• meaning of file identification: find the only identified file in the computer.

• composition of file ID: file path, file name, trunk and file extension.

• the operating system manages data in file units.

file type

According to the logical storage structure of data, people divide the files in the computer into text files and binary files.

Text file: specially stores text character data.

Binary file: it cannot be read and written directly by word processing program. You must first understand its structure and serialization rules, and then design correct deserialization rules to correctly obtain file information.

The division of binary file and text file is based on data logical storage structure rather than physical storage structure. The data in the computer is stored in binary form at the physical level.

Standard documents

Three standard files are defined in the sys module of Python:

• stdin (standard input file). The standard input file corresponds to an input device, such as a keyboard.
• stdout (standard output file).
• stderr (standard error file). Standard output files and standard error files correspond to output devices, such as displays.

After importing the sys module in the interpreter, you can operate on the standard file.

import sys
file = sys.stdout
file.write("hello")

7.2 basic operation of documents

1. Open the file

The built-in function open() is used to open files. The declaration of this method is as follows:

open(file, mode='r', buffering=-1) 

2. Close the file

Python can close the file through the close() method, or use the with statement to close the file automatically.

1) close() method

• close() Method is a built-in method of a file object.
file.close()

(2) with statement

• with language Sentence predictable Define cleanup operations to automatically close files Shut.
with open('a.txt') as f:
        pass

7.2. 2. File reading and writing

Python provides a series of methods for reading and writing files, including read(), readline(), readlines() methods for reading files and write(), writelines() methods for writing files

1. Read file -- read() method

The read() method can read the data of the specified byte from the specified file. Its syntax format is as follows:

read(n=-1)

with open('file.txt', mode='r') as f:
    print(f.read(2))   						# Read two bytes of data
    print(f.read())    						# Read all remaining data

1. Read file -- readline() method

The readline() method can read a line of data from the specified file. Its syntax format is as follows:

readline()

with open('file.txt', mode='r', encoding='utf-8') as f:
    print(f.readline())
    print(f.readline())

1. Read file -- readline() method

The readlines() method can read all the data in the file at once. If the reading is successful, the method will return a list, and each line in the file corresponds to an element in the list. The syntax format is as follows:

readlines(hint=-1)  

(1) The unit of t is bytes, which is used to control the number of rows to be read

(2) The total size of the data in exceeds hint bytes, and readlines() will not read more rows.

with open('file.txt', mode='r', encoding='utf-8') as f:
    print(f.readlines())                                                                 # Use the readlines() method to read the data

2. Write file - write() method

The write() method can write the specified string to a file. Its syntax format is as follows:

write(data)

string = "Here we are all, by day; by night."		# character string
with open('write_file.txt', mode='w', encoding='utf-8') as f:	
    size = f.write(string)					# Write string
    print(size)						# Number of bytes printed

2. Write file -- writelines() method

The writelines() method is used to write the line list to a file. Its syntax format is as follows:

writelines(lines)

string = "Here we are all, by day;\nby night we're hurl'd By dreams, 
each one into a several world."
with open('write_file.txt', mode='w', encoding='utf-8') as f:
    f.writelines(string)

7.2. 3. Fixed writing, reading and writing of documents

2.seek() method

Python provides the seek () method, which can be used to control the reading and writing position of the file and realize the random reading and writing of the file. The syntax format of the seek() method is as follows:

seek(offset, from)

Offset: indicates the offset, that is, the number of bytes to be moved in the read / write position.

pfrom: used to specify the file read / write location. The values of this parameter are 0, 1, and 2.

2.seek() method

with open('file.txt') as f:
    f.seek(5,0)								# Move 5 bytes relative to the beginning of the file
    f.seek(3,1)

2.seek() method

with open('file.txt','rb') as f:
    f.seek(5,0)
    f.seek(3,1)

7.3. 3 format of multidimensional data

Type comparison table of Python object and JSON data conversion

 1.dumps() function

import json
pyobj = [[1, 2, 3], 345, 23.12, 'qwe', {'key1':(1,2,3), 'key2':(2,3,4)}, True, False, None]
 jsonstr = json.dumps(pyobj)
print(jsonstr)
 [[1, 2, 3], 345, 23.12, "qwe", {"key1": [1, 2, 3], "key2": [2, 3, 4]}, true, false, null]

1..loads() function

import json
pyobj = [[1, 2, 3], 345, 23.12, 'qwe', {'key1':(1,2,3), 'key2':(2,3,4)}, True, False, None]
 jsonstr = json.dumps(pyobj)
 print(jsonstr)
 [[1, 2, 3], 345, 23.12, "qwe", {"key1": [1, 2, 3], "key2": [2, 3, 4]}, true, false, null]

Topics: Python Back-end