SoFunction
Updated on 2024-12-20

Python basic tuple and file knowledge summary

leading principles

在这里插入图片描述

Python file types and summary

在这里插入图片描述

I. Tuples

1 Characteristics

1. An ordered collection of arbitrary objects
2. Access via subscripts
3. Immutable
4. Fixed length, any type, any nesting

>>> t = (1,2,3,4,5)
>>> t[0] = 2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment

2 Declaration

(value1,value2,…)

3 Operation

(val):Find Index
(val): statistical data

>>> t
(1, 2, 3, 4, 5)
>>> (3)
2
>>> (3)
1

tuple code

(1,2) # Define a tuple
(1, 2)
(1,2)+(3,4)# Add tuple
(1, 2, 3, 4)

t=[1,2,3,4,5]
res=[x**2 for x in t] # Calculate the square of the element in t and put it in res
res
[1, 4, 9, 16, 25]
(3) # Retrieve the location of 3
2
(3) # of 3's in the tuple t
1

from collections import namedtuple # Introduce namedtuple to assign values to employees
employee=namedtuple("employee",["named","age","department","salary"]) #Define an employee template
Jerry=employee("Jerry",30,"Finance Department","9000.00")# Assign a value to an employee named Jerry
Jerry
employee(named='Jerry', age=30, department='Ministry of Finance', salary='9000.00')
 #Read Jerry's age
30

在这里插入图片描述

Caveats:List Tuple Conversion

在这里插入图片描述

tuple parsing

在这里插入图片描述

Modification of the internal list of tuples

在这里插入图片描述

II. Documentation

1 Basic syntax

file = open('filename', mode)

Three models

mode:r ,w ,a

>>> myfile = open('','w') #if not,Automatic file creation

2 Operation

read, readlines, close method

>>> myfile = open('','w')
>>> ("Hello, my name is Selia\n.") #Write operations
10
>>> ()
>>> f = open('')
>>> ()
''Hello, my name is Selia\n''
>>> ()
''
>>> f = open('')
>>> ()                      #readline reads one line at a time and returns a string.
''Hello, my name is Selia\n''
>>> ()
''
>>> l = open('').readlines() #readline reads all lines at once and returns a list.
>>> l
[''Hello, my name is Selia\n'']

with open() as ... used to temporarily open the file, after the end of the automatic close to release resources (recommended this way to open the file for operation)

>>> f = open('')
>>> ()
''Hello, my name is Selia\n''
>>> ()
''
>>> f = open('')
>>> ()                      #readline reads one line at a time and returns a string.
''Hello, my name is Selia\n''
>>> ()
''
>>> l = open('').readlines() #readline reads all lines at once and returns a list.
>>> l
[''Hello, my name is Selia\n'']

grid
amiable
surname Yun
form of divination
relationship between cousins etc on the paternal side of a family

file permissions

  • rb opens a file in binary format for read-only use. The file pointer will be placed at the beginning of the file. Typically used for non-text files such as images.

Note: Binary files represent the contents as a special bytes string type.

# file = open("demo1/","rb")
file = open("demo1/","rb")
ret = ()  #b'huangzhi'   huangzhi
print(ret)
()

r+ Opens a file for reading and writing. The file pointer will be placed at the beginning of the file.

file = open("demo1/","r+")
# ret = () # read all of it
# print(ret) 
("guyin") # Write from scratch, the original content will be gradually overwritten
()
  • rb+ opens a file in binary format for reading and writing. The file pointer will be placed at the beginning of the file. Generally used for non-text files such as images.
  • wb Opens a file in binary format for writing only. If the file already exists the file is opened and edited from the beginning, i.e. the original content is deleted. If the file does not exist, a new file is created. Generally used for non-text files such as images.
from  import img2
file = open("demo1/","wb")
(img2)
()
  • w+ Opens a file for reading and writing. If the file already exists, the file is opened and edited from the beginning, i.e. the original content is deleted. If the file does not exist, a new file is created.
file = open("demo1/","w+")
("hello world")
ret = ()
print(ret)
()
  • a Opens a file for appending. If the file already exists, the file pointer will be placed at the end of the file.

That is, the new content will be written after the existing content. If the file does not exist, a new file is created for writing.

#Add "guyin" to demo1.
# file = open("demo1/","a")
file = open("demo1/","a")
("guyin")
()
  • ab Opens a file in binary format for appending. If the file already exists, the file pointer will be placed at the end of the file. That is, the new content will be written after the existing content. If the file does not exist, a new file is created for writing.
  • a+ Opens a file for reading and writing. If the file already exists, the file pointer will be placed at the end of the file.
  • The file is opened in append mode. If the file does not exist, a new file is created for reading and writing.
file = open("demo1/","a+")
("yangyong")
ret = ()
print(ret)
()
  • ab+ Opens a file in binary format for appending. If the file already exists, the file pointer is placed at the end of the file. If the file does not exist, a new file is created for reading and writing.

III. pickle stores and reads python objects

dump(object, destination file)
load(file)

f = open('','wb')
>>> import pickle
>>> d = {'a':1,'b':2}
>>> (d,f)
>>> ()
 
 
>>> f = open('','rb')
>>> data = (f)
>>> data
{'a': 1, 'b': 2}

在这里插入图片描述

IV. Summary of types

在这里插入图片描述

To this article on the basis of Python tuple and file knowledge summarizes the article is introduced to this, more related Python tuple and file content please search my previous posts or continue to browse the following related articles I hope you will support me more in the future!