Xem trước
Để viết một đối tượng & nbsp; Đối với một tệp nhị phân được mở trong chế độ ghi, chúng ta nên sử dụng hàm dump [] của mô -đun pickle theo cú pháp sau:dump[ ] function of pickle module as per the following syntax : dưa chua.dump [,] Ví dụ: nếu bạn có một tệp mở trong tay cầm F1 như
F1 = Mở [Hồi File1.txt ,, WB WB]
1. Chương trình Python để viết danh sách là Dist1 trong tệp có tên File1.txt
import pickle f1=open["file1.txt","wb"] list1=[1,'Lovejot','Teacher'] pickle.dump[list1,f1] f1.close[]
2. & nbsp; chương trình python cho & nbsp; viết một tuple cụ thể là t1 trong tệp có tên file2.txtPython program to write a tuple namely t1 in the file named file2.txt
import pickle f2=open["file2.txt","wb"] t1=[2,'Sumit','XII'] pickle.dump[t1,f2] f2.close[]
3. & nbsp; chương trình python cho & nbsp; viết một từ điển là D1 trong tệp có tên File3.txtPython program to write a dictionary namely d1 in the file named file3.txt
import pickle f3=open["file3.txt","wb"] d1={'Rollno':1, name:'Anil'} pickle.dump[d1,f3] f3.close[]
4. & nbsp; Chương trình Python cho & nbsp; tạo một tệp nhị phân có tên nhân viên.dat và viết chi tiết của nhân viên có sẵn dưới dạng từ điển.Python program to create a binary file named employee.dat and write details of employees available in the form of dictionaries.
import pickle # dictionary objects E1={ ‘Empno’ :101, ‘Name’ : ‘Ramesh’ , ‘Salary’ : 17000} E2={ ‘Empno’ :102, ‘Name’ : ’’ , ‘Age’ : 30, ‘Salary’ : 18000} #open file in write mode file1 = open [‘Employee.dat’ , ‘wb’] #write to the file pickle.dump [E1 ,file1] pickle.dump [E2 , file1] #close file file1.close[ ]
5. & nbsp; Chương trình Python cho & nbsp; tạo một tệp nhị phân có tên student.dat. Dữ liệu của học sinh phải được đặt trong thời gian chạy thì nó nên được lưu trữ trong hồ sơ. Cấu trúc của dữ liệu là [rollno, tên, nhãn hiệu].Python program to create a binary file named student.dat . Data of students must be put at run time then it should be stored in file. The structure of data is [rollno, name, marks].
import pickle s={ } #declare empty dictionary file2 = open[‘student.dat’ , ‘wb’] #open file R = int [input [“Enter roll number=“ ] ] N = input [“Enter name =“ ] M = float[input [“Enter marks =“ ] # add read data into dictionary S [‘Rollno’] = R S [‘Name’] = N S[‘Marks’] = M # now write into the file pickle.dump [s ,file2] file2.close[ ]
Nối lại các bản ghi trong tệp nhị phân trong Python
Việc nối thêm các bản ghi trong các tệp nhị phân tương tự như bản ghi ghi nhưng có hai sự khác biệt:
tôi. Chúng ta cần mở tệp nhị phân ở chế độ phụ trợ [AB AB]. ii. Nếu tệp không thoát ra, nó sẽ tạo một tệp mới. iii. Nếu tệp đã thoát ra, nó sẽ viết bản ghi mới ở cuối tệp hiện có.
ii. If file doesn’t exit, it will create a new file .
iii. If file already exits, it will write new record at the end of existing file.
# Chương trình Python để nối một danh sách cụ thể là List1 trong tệp có tên File1.txtappend a list namely list1 in the file named file1.txt
import pickle f1=open["file1.txt","ab"] list1=[2,'Ayush','Student'] pickle.dump[list1,f1] f1.close[]
Chương trình #PyThon TO & NBSP; Nối một Tuple là T1 trong tệp có tên File2.txt append a tuple namely t1 in the file named file2.txt
import pickle f2=open["file2.txt","ab"] t1=[3,'Sunita','XI'] pickle.dump[t1,f2] f2.close[]
Chương trình #Python để nối một từ điển là D1 trong tệp có tên File3.txtappend a dictionary namely d1 in the file named file3.txt
import pickle f3=open["file3.txt","ab"] d1={'Rollno':2, name:'Sumit'} pickle.dump[d1,f3] f3.close[]
Chương trình #PyThon để nối các hồ sơ sinh viên vào một tệp hiện có Student.dat bằng cách nhận đầu vào từ người dùng.existing file student.dat by getting input from user.
import pickle Students ={ } #empty dictionary created to store records file1 = open ['Student.dat', 'ab'] choice = 'y' while choice == 'y' : #Read values of rollno, name and marks R=int[input["Enter roll number of student = "]] N=input["Enter name ="] M=float [input["Enter marks = "]] #put values of variables R,N, M in dictionary Students['Rollno'] =R Students['Name'] =N Students['Marks'] =M pickle.dump [Students, file1] #Write record into the file choice=input["Do you want to add more records [y/n]] file1.close[ ]
Đầu ra
Enter roll number of student = 101 Enter name =anita Enter marks = 88 Do you want to add more records [y/n]…y Enter roll number of student = 102 Enter name =sunil Enter marks = 56 Do you want to add more records [y/n]n
Nhấn vào đây để kiểm tra xử lý hồ sơ trong Python
Sách phổ biến của Khoa học Máy tính
Bạn đã đăng ký thành công.
Có một lỗi trong khi cố gắng gửi yêu cầu của bạn. Vui lòng thử lại.