Chủ đề này cho thấy cách bạn có thể lưu dữ liệu trong các tệp nhị phân mà không cần sử dụng các mô -đun Pickle hoặc Struct tiêu chuẩn.
Nội dung
- 1. Khái niệm về các tập tin nhị phân. Trình bày thông tin trong các tệp nhị phân
- 2. Viết / đọc một danh sách chứa số thực. Thí dụ
- 3. Viết/đọc một tuple chứa chuỗi ký tự. Thí dụ
- 4. Viết / đọc một bộ chứa số thực. Thí dụ
- 5. Viết/đọc một ma trận hai chiều của các hàng có kích thước nhất định. Thí dụ
- 6. Viết/đọc một từ điển. Thí dụ
- 7. Sao chép một tệp nhị phân sang một tệp khác
- 8. Kết hợp hai tệp nhị phân
- Chủ đề liên quan
Tìm kiếm các trang web khác:
1. Khái niệm về các tập tin nhị phân. Trình bày thông tin trong các tệp nhị phân2. Viết / đọc một danh sách chứa số thực. Thí dụ
3. Viết/đọc một tuple chứa chuỗi ký tự. Thí dụopen[] function, whose mode parameter contains the character ‘b’. More details about opening/closing binary files are described here.
4. Viết / đọc một bộ chứa số thực. Thí dụ‘\n’.
5. Viết/đọc một ma trận hai chiều của các hàng có kích thước nhất định. Thí dụ
# Python. Work with binary files # Open binary file for reading f = open['myfile1.bin', 'rb'] # Get a string from binary file d = f.read[] # Display this string. # The output will be as a string of characters print["d = ", d] # d = b'\x80\x03]q\x00[K\x01\x88G@\x07\n=p\xa3\xd7\ne.' # If print as a separate character, # then the character code will be displayed - as an integer print["d[5] = ", d[5]] # d[5] = 40 print["d[0] = ", d[0]] # d[0] = 128 # Use bin function for single character print[bin[d[2]]] # 0b1011101 f.close[]
6. Viết/đọc một từ điển. Thí dụ
d = b'\x80\x03]q\x00[K\x01\x88G@\x07\n=p\xa3\xd7\ne.' d[5] = 40 d[0] = 128 0b1011101
7. Sao chép một tệp nhị phân sang một tệp khác
- 8. Kết hợp hai tệp nhị phân
- Chủ đề liên quan
Tìm kiếm các trang web khác:⇑
2. Viết / đọc một danh sách chứa số thực. Thí dụ# Binary files Writing / reading a list of real numbers # 1. Given list of real numbers L = [1.5, 2.8, 3.3] # 2. File writing # 2.1. Open file for writing f = open['myfile3.bin', 'wb'] # 2.2. Bypass list and write data to a file for item in L: # add the character '\ n' so that numbers can be recognized s = str[item] + '\n' # Encode [] method - converts a string to a sequence of bytes bt = s.encode[] f.write[bt] # write[] method - write to file # 2.3. Close file f.close[]; # 3. Read list from binary file 'myfile3.bin' # 3.1. Create an empty list L2 = [] # 3.2. Open file for reading f = open['myfile3.bin', 'rb'] # 3.3. Bypass lines of a file, converting and adding to the list L2 for ln in f: x = float[ln] # get a number L2 = L2 + [x] # Add number to list # 3.4. Output the list print["L2 = ", L2] # L2 = [1.5, 2.8, 3.3] # 3.5. Close file f.close[];
6. Viết/đọc một từ điển. Thí dụ
L2 = [1.5, 2.8, 3.3]
Tìm kiếm các trang web khác:⇑
3. Viết/đọc một tuple chứa chuỗi ký tự. Thí dụ4. Viết / đọc một bộ chứa số thực. Thí dụ‘\n’. Thus, it is possible to write and read information without losing its structure.
# Binary files. Writing / reading a tuple containing character strings # 1. The specified tuple with strings T = [ 'abc', 'def', 'ghi', 'jk lmn'] # 2. Write tuple T to file 'myfile5.bin' # 2.1. Open file for writing f = open['myfile5.bin', 'wb'] # 2.2. Tuple bypass cycle for item in T: bt = [item + '\n'].encode[] # convert [str + '\n'] => bytes f.write[bt] # write bt to file # 2.3. Close file f.close[]; # 3. Read tuple from binary file 'myfile5.bin' # 3.1. Open file for reading f = open['myfile5.bin', 'rb'] # 3.2. A new tuple T2 = [] # 3.3. Read data from file for line in f: s = line.decode[] # convert bytes=>str s = s[:-1] # Remove the last character T2 = T2 + [s,] # Add string s to tuple # 3.4. Print the tuple print["T2 = ", T2] # 3.5. Close file f.close[];
6. Viết/đọc một từ điển. Thí dụ
T2 = ['abc', 'def', 'ghi', 'jk lmn']
Tìm kiếm các trang web khác:⇑
4. Viết / đọc một bộ chứa số thực. Thí dụ5. Viết/đọc một ma trận hai chiều của các hàng có kích thước nhất định. Thí dụ
# Binary files. Writing/reading a set that contains real numbers # 1. Given set M = { 0.2, 0.3, 0.8, 1.2, 1.77 } # 2. Writing the set M to the file 'myfile6.bin' # 2.1. Open file for writing f = open['myfile6.bin', 'wb'] # 2.2. The loop of bypass the set for item in M: s = str[item] + '\n' # convert float=>str + '\n' bt = s.encode[] # convert str=>bytes f.write[bt] # write bt to file # 2.3. Close file f.close[]; # 3. Read set from binary file 'myfile6.bin' # 3.1. Open file for reading f = open['myfile6.bin', 'rb'] # 3.2. Create a new set M2 = set[] # 3.3. Read data from file for line in f: x = float[line] # convert bytes=>x M2 = M2.union[{x}] # add x to the set # 3.4. Print the set print["M2 = ", M2] # 3.5. Close file f.close[]
6. Viết/đọc một từ điển. Thí dụ
M2 = {0.2, 0.8, 0.3, 1.77, 1.2}
7. Sao chép một tệp nhị phân sang một tệp khác
0.2 0.8 1.77 0.3 1.2
Tìm kiếm các trang web khác:⇑
5. Viết/đọc một ma trận hai chiều của các hàng có kích thước nhất định. Thí dụ6. Viết/đọc một từ điển. Thí dụ
# Binary files. Writing/reading a matrix that contains rows # 1. Given matrix of rows 3*4 in size MATRIX = [ [ 'aa', 'ab', 'ac', 'ad'], [ 'ba', 'bb', 'bc', 'bd'], [ 'ca', 'cb', 'cc', 'cd'] ] # 2. Writing MATRIX matrix to the file 'myfile7.bin' # 2.1. Open file for writing f = open['myfile7.bin', 'wb'] # 2.2. First, write the size of the matrix m = 3 n = 4 # convert m, n to str type sm = str[m] + '\n' sn = str[n] + '\n' # convert str to bytes bm = sm.encode[] bn = sn.encode[] # write matrix sizes to file f.write[bm] f.write[bn] # 2.3. The loop of matrix traversal for row in MATRIX: # here you just need to write lines with the character '\n' for item in row: item = item + '\n' bt = item.encode[] # str=>bytes f.write[bt] # write bt to file # 2.3. Close file f.close[]; # 3. Read matrix from binary file 'myfile7.bin' # 3.1. Open file for reading f = open['myfile7.bin', 'rb'] # 3.2. A new matrix MATRIX2 = [] # 3.3. Read data from file # 3.3.1. First, read size s = f.readline[] m2 = int[s] s = f.readline[] n2 = int[s] # 3.3.2. Loop of reading the lines and the creation of a matrix size m2*n2 i = 0 while i < m2: # m2 rows in matrix row = [] # one row in list j = 0 while j < n2: bs = f.readline[] # read one element of type bytes s = bs.decode[] # convert bytes=>str s = s[:-1] # remove '\n' row += [s] # add to the list j = j+1 MATRIX2 += [row] # add one row of the list to the matrix i = i+1 # 3.4. Display new matrix i = 0 while i < m2: print["MATRIX2[", i, "] = ", MATRIX2[i]] i = i+1 # 3.5. Close file f.close[]
6. Viết/đọc một từ điển. Thí dụ
d = b'\x80\x03]q\x00[K\x01\x88G@\x07\n=p\xa3\xd7\ne.' d[5] = 40 d[0] = 128 0b10111010
7. Sao chép một tệp nhị phân sang một tệp khácmyfile7.txt
d = b'\x80\x03]q\x00[K\x01\x88G@\x07\n=p\xa3\xd7\ne.' d[5] = 40 d[0] = 128 0b10111011
Tìm kiếm các trang web khác:⇑
6. Viết/đọc một từ điển. Thí dụ7. Sao chép một tệp nhị phân sang một tệp khác
d = b'\x80\x03]q\x00[K\x01\x88G@\x07\n=p\xa3\xd7\ne.' d[5] = 40 d[0] = 128 0b10111012
6. Viết/đọc một từ điển. Thí dụ
d = b'\x80\x03]q\x00[K\x01\x88G@\x07\n=p\xa3\xd7\ne.' d[5] = 40 d[0] = 128 0b10111013
7. Sao chép một tệp nhị phân sang một tệp khácmyfile8.txt file
d = b'\x80\x03]q\x00[K\x01\x88G@\x07\n=p\xa3\xd7\ne.' d[5] = 40 d[0] = 128 0b10111014
Tìm kiếm các trang web khác:⇑
7. Sao chép một tệp nhị phân sang một tệp khácd = b'\x80\x03]q\x00[K\x01\x88G@\x07\n=p\xa3\xd7\ne.' d[5] = 40 d[0] = 128 0b10111015
Tìm kiếm các trang web khác:⇑
Python có các công cụ để làm việc với các tệp nhị phân. Tệp nhị phân sử dụng chuỗi loại byte. Điều này có nghĩa là khi đọc dữ liệu nhị phân từ một tệp, một đối tượng loại byte được trả về.Tệp nhị phân được mở bằng hàm Open [], có tham số chế độ chứa ký tự ‘B ,. Thông tin chi tiết về việc mở/đóng các tập tin nhị phân được mô tả ở đây.
d = b'\x80\x03]q\x00[K\x01\x88G@\x07\n=p\xa3\xd7\ne.' d[5] = 40 d[0] = 128 0b10111016
Tìm kiếm các trang web khác:⇑
Chủ đề liên quan
- Tìm kiếm các trang web khác:open[], close[]
- Python có các công cụ để làm việc với các tệp nhị phân. Tệp nhị phân sử dụng chuỗi loại byte. Điều này có nghĩa là khi đọc dữ liệu nhị phân từ một tệp, một đối tượng loại byte được trả về.