The input and output of information are collectively referred to as Input/Output, which is abbreviated as I/O. in addition, I/O not only refers to keyboard input, screen printout, but also file input and output.
1, Input and Output
1) input
""" !!!Return string type To get the required data type Data type cast is required """ input()
2) output
a. Normal output
print(...)
Multiple variables are separated by commas. The print function will print in turn. If a comma is encountered, it will be replaced by a space, and the print function can calculate automatically.
b. Format output
Usage: add the corresponding format character after% to occupy the space, and then replace and output.
Example:
# %s stands for string% d stands for value name,age='wy',20 print('name: %s, age: %d' % (name,age)) print('%s,%d' %(name,age)) #output # name: wy, age: 20 # wy,20
2, File I/O
Define data file (i.e. file path) - get file object - read file content - close file object
1) Function description
open,read,readline,readlines,write,close
#Open / create file file_object=open(File path, access mode) # Return file object """ Read mode: 1.read(size) Read all data until the defined maximum number of bytes is reached size,If omitted, all data in the file will be read by default Read by byte Return: content string. All lines are merged into one string 2.readline() Read a row of data until the defined maximum number of bytes is reached Read by line Return: content string 3.readlines(size) Read all the data of the file until the defined maximum number of bytes is reached size,If omitted, all data in the file will be read by default Read line by line Returns a list of contents, with each row of data as an element of the list """ file_object.read('data.txt','r+') file_object.read() #Reading byte by byte is inefficient file_object.readlines() #Read line by line, but the returned is in the form of a list. The elements of the list are the contents of the file line by line file_object.write(str) #Use the file object to write the content of character type. It is empty by default file_object.close() #Close file #with open form # Read entire file with open('pi_digits.txt') as file_object: contents = file_object.read() print(contents) ''' 1.with open The form does not need to be called close()Close the file, python It will turn off automatically 2.read Function reads the contents of the entire file,Compared with the original file, there will be an empty line at the end—— because read() When the end of the file is reached, an empty string is returned, and when the empty string is displayed, it is an empty line. To delete the extra blank lines, click print Used in statements rstrip() : ''' with open('pi_digits.txt') as file_object: contents = file_object.read() print(contents.rstrip()) #Read file line by line filename = 'pi_digits.txt' with open(filename) as file_object: for line in file_object: print(line) #The object returned by the open function is only valid within the with code block with open(filename) as file_object: lines = file_object.readlines() for line in lines: print(line.rstrip())
2) File path
1. When the file is in the same directory as the current program file, only the file name is required,
2. Relative path: if it is not in the same directory, but in a subdirectory of the directory, you can use the relative path (relative to the directory where the currently running program is located)
3. Absolute path
#\It needs to be represented by \ with open('C:\\Users\\ann\\Desktop\\11.txt') as file_object: contents=file_object.read() print(contents)
3) Pattern
Open (file path, mode)
'w' means to open the file by writing and overwrite the write;
----File object name returned by open. Write (content written)
'r' means to open the file by reading;
'a' means append
'r +' indicates both read and write
'a +' indicates that it can be read and appended
be careful:
1) python can only write strings to text files and store numeric data in text files, which must be converted to string format using the str() function
2) write does not add a newline character to the end of the written text, so you need to use symbols such as spaces, tabs, blank lines, etc. to format the output
3) Write the file in w mode. If the file does not exist, it will be created automatically; If it already exists, it will be cleared first and then written. If you don't want the previous content to be overwritten, you can write it by appending
4) Serialization
We can write a string to a local file, but it cannot be an object (list, dictionary, custom class object, etc.)
TypeError: write() argument must be str, not list, so the object needs to be serialized before it can be written to the file. Python provides JSON module to realize data serialization and deserialization.
\quad
JSON (JavaScript object notation) is a lightweight data exchange library. The essence of JSON is a string
1. Serialization:
Serialize the data to be stored, remove the original data type, change it into a string, and then write it to the file
#dump and dumps json.dumps(a) #Convert object a to a string 1.dumps(): import json #import module json first with open('1.json','w') as file: a=[1,2,5] r=json.dumps(a) print(r,type(r)) #[1, 2, 5] <class 'str'> file.write(r) 2.dump() #json.dump() accepts two parameters: the data to store and the file object used to store the data import json with open('1.json','w') as file: a=[1,2,5] json.dump(a,file)
2. Deserialization:
Read the data from the file, deserialize it, and "recover" the type of data
#loads,load 1.loads() import json with open('1.json') as file: content=file.read() print(content,' ',type(content)) content=json.loads(content) print(content,' ',type(content)) #output [1, 2, 5] <class 'str'> [1, 2, 5] <class 'list'> 2.load() import json with open('1.json') as file: content=json.load(file) print(content,' ',type(content)) #output [1, 2, 5] <class 'list'>