6
Mới! Lưu câu hỏi hoặc câu trả lời và sắp xếp nội dung yêu thích của bạn. Tìm hiểu thêm.
Learn more.
Tôi có một tập lệnh rất đơn giản trong MATLAB mở tệp hình ảnh nhị phân 'thô' và hiển thị nó. Điều này có dễ dàng tái tạo bằng cách sử dụng Numpy trong Python không? Tôi đã bắt gặp các bài viết khác nhau thảo luận về việc giải nén, xử lý endian, chỉ định bộ đệm, v.v.
>> fileID = fopen['sampleX3.raw','rb']
fileID =
1
>> A = fread[fileID,[1024,1024],'int16'];
size[A]
ans =
1024 1024
>> max[max[A]]
ans =
12345
>> close all; figure; imagesc[A];
Đã hỏi ngày 4 tháng 7 năm 2013 lúc 23:39Jul 4, 2013 at 23:39
1
Điều này sẽ làm điều tương tự bằng cách sử dụng Numpy và Matplotlib:
import numpy as np
from matplotlib import pylab as plt
A = np.fromfile[filename, dtype='int16', sep=""]
A = A.reshape[[1024, 1024]]
plt.imshow[A]
Tôi cảm thấy bắt buộc phải đề cập rằng sử dụng các tệp nhị phân thô để lưu trữ dữ liệu nói chung là một ý tưởng tồi.
Đã trả lời ngày 4 tháng 7 năm 2013 lúc 23:57Jul 4, 2013 at 23:57
Bi Ricobi RicoBi Rico
24.7K3 Huy hiệu vàng 50 Huy hiệu bạc72 Huy hiệu đồng3 gold badges50 silver badges72 bronze badges
2
Tệp nhị phân là các tệp không phải là tệp văn bản bình thường. Ví dụ: Một tệp hình ảnh. Các tệp này cũng được lưu trữ dưới dạng chuỗi byte trong đĩa cứng máy tính. Các loại tệp nhị phân này không thể được mở ở chế độ bình thường và đọc dưới dạng văn bản.Image File. These files are also stored as a sequence of bytes in the computer hard disk. These types of binary files cannot be opened in the normal mode and read as text.
Bạn có thể đọc tệp nhị phân bằng cách mở tệp ở chế độ nhị phân bằng cách sử dụng
1.try:
with open["c:\temp\Binary_File.jpg", "rb"] as f:
byte = f.read[1]
while byte:
# Do stuff with byte.
byte = f.read[1]
print[byte]
except IOError:
print['Error While Opening the file!']
Khi làm việc với các vấn đề như phân loại hình ảnh trong học máy, bạn có thể cần mở tệp ở chế độ nhị phân và đọc các byte để tạo các mô hình ML. Trong tình huống này, bạn có thể mở tệp ở chế độ nhị phân và đọc tệp dưới dạng byte. Trong trường hợp này, việc giải mã byte cho các ký tự có liên quan sẽ không được thử. Mặt khác, khi bạn mở một tệp bình thường ở chế độ đọc thông thường, các byte sẽ được giải mã thành chuỗi hoặc các ký tự có liên quan khác dựa trên mã hóa tệp.image classification in Machine learning, you may need to open the file in binary mode and read the bytes to create ML models. In this situation, you can open the file in binary mode, and read the file as bytes. In this case, decoding of bytes to the relevant characters will not be attempted. On the other hand, when you open a normal file in the normal read mode, the bytes will be decoded to string or the other relevant characters based on the file encoding.
Nếu bạn vội vàng…
Bạn có thể mở tệp bằng phương thức
try:
with open["c:\temp\Binary_File.jpg", "rb"] as f:
byte = f.read[1]
while byte:
# Do stuff with byte.
byte = f.read[1]
print[byte]
except IOError:
print['Error While Opening the file!']
2 bằng cách truyền tham số try:
with open["c:\temp\Binary_File.jpg", "rb"] as f:
byte = f.read[1]
while byte:
# Do stuff with byte.
byte = f.read[1]
print[byte]
except IOError:
print['Error While Opening the file!']
3 để mở nó ở chế độ nhị phân và đọc các byte tệp. try:
with open["c:\temp\Binary_File.jpg", "rb"] as f:
byte = f.read[1]
while byte:
# Do stuff with byte.
byte = f.read[1]
print[byte]
except IOError:
print['Error While Opening the file!']
3 parameter to open it in binary mode and read the file bytes.try:
with open["c:\temp\Binary_File.jpg", "rb"] as f:
byte = f.read[1]
while byte:
# Do stuff with byte.
byte = f.read[1]
print[byte]
except IOError:
print['Error While Opening the file!']
4 Mở tệp nhị phân ở chế độ đọc.________ 25, để chỉ định để mở tệp trong modeb đọc - để chỉ định nó là một tệp nhị phân. Không có việc giải mã byte để cố gắng chuỗi sẽ được thực hiện.
b – To specify it’s a
binary file. No decoding of bytes to string attempt will be made.
Thí dụ
Ví dụ dưới đây đọc tệp một byte tại một thời điểm và in byte.
try:
with open["c:\temp\Binary_File.jpg", "rb"] as f:
byte = f.read[1]
while byte:
# Do stuff with byte.
byte = f.read[1]
print[byte]
except IOError:
print['Error While Opening the file!']
Nếu bạn muốn hiểu chi tiết, hãy đọc trên
Trong hướng dẫn này, bạn sẽ học cách đọc các tệp nhị phân theo những cách khác nhau.
- Đọc Byte File Byte by Byte
- Python đọc tệp nhị phân vào mảng byte
- Python đọc tệp nhị phân vào mảng numpy
- Đọc từng dòng tệp nhị phân
- Đọc tệp nhị phân đầy đủ trong một lần bắn
- Python đọc tệp nhị phân và chuyển đổi thành ASCII
- Đọc tệp nhị phân vào DataFrame
- Đọc tiêu đề bỏ qua tệp nhị phân
- Readind Tệp nhị phân bằng cách sử dụng dưa chua
- Sự kết luận
Đọc Byte File Byte by Byte
Python đọc tệp nhị phân vào mảng byte
Python đọc tệp nhị phân vào mảng numpy“rb” which means opening the file in reading mode and denoting it’s a binary file. In this case, decoding of the bytes to string will not be made. It’ll just be read as bytes.
Đọc từng dòng tệp nhị phân
Đọc tệp nhị phân đầy đủ trong một lần bắn1 ensures one byte is read during each
try:
with open["c:\temp\Binary_File.jpg", "rb"] as f:
byte = f.read[1]
while byte:
# Do stuff with byte.
byte = f.read[1]
print[byte]
except IOError:
print['Error While Opening the file!']
8 method call.Thí dụ
try:
with open["c:\temp\Binary_File.jpg", "rb"] as f:
byte = f.read[1]
while byte:
# Do stuff with byte.
byte = f.read[1]
print[byte]
except IOError:
print['Error While Opening the file!']
Ví dụ dưới đây đọc tệp một byte tại một thời điểm và in byte.
b'\xd8'
b'\xff'
b'\xe0'
b'\x00'
b'\x10'
b'J'
b'F'
b'I'
b'F'
b'\x00'
b'\x01'
b'\x01'
b'\x00'
b'\x00'
b'\x01'
b'\x00'
b'\x01'
b'\x00'
b'\x00'
b'\xff'
b'\xed'
b'\x00'
b'|'
b'P'
b'h'
b'o'
b't'
b'o'
b's'
b'h'
b'o'
b'p'
b' '
b'3'
b'.'
b'0'
b'\xc6'
b'\xb3'
b'\xff'
b'\xd9'
b''
Python đọc tệp nhị phân vào mảng byte
Python đọc tệp nhị phân vào mảng numpyread the binary files into a byte array.
Đọc từng dòng tệp nhị phân“
9“ mode.try:
with open["c:\temp\Binary_File.jpg", "rb"] as f:
byte = f.read[1]
while byte:
# Do stuff with byte.
byte = f.read[1]
print[byte]
except IOError:
print['Error While Opening the file!']
Đọc tệp nhị phân đầy đủ trong một lần bắn
Python đọc tệp nhị phân và chuyển đổi thành ASCII
Đọc tệp nhị phân vào DataFrame
Thí dụ
try:
with open["c:\temp\Binary_File.jpg", "rb"] as f:
mybytearray = bytearray[]
# Do stuff with byte.
mybytearray+=f.read[1]
mybytearray+=f.read[1]
mybytearray+=f.read[1]
mybytearray+=f.read[1]
mybytearray+=f.read[1]
print[mybytearray]
except IOError:
print['Error While Opening the file!']
Ví dụ dưới đây đọc tệp một byte tại một thời điểm và in byte.
bytearray[b'\xff\xd8\xff\xe0\x00\x10']
Python đọc tệp nhị phân vào mảng numpy
Đọc từng dòng tệp nhị phânread the binary file into a NumPy array.
Đọc tệp nhị phân đầy đủ trong một lần bắn
Python đọc tệp nhị phân và chuyển đổi thành ASCII
Đọc tệp nhị phân vào DataFrame
Đọc tiêu đề bỏ qua tệp nhị phân
Readind Tệp nhị phân bằng cách sử dụng dưa chua
Sự kết luận
Thí dụ
import numpy as np
dtype = np.dtype['B']
try:
with open["c:\temp\Binary_File.jpg", "rb"] as f:
numpy_data = np.fromfile[f,dtype]
print[numpy_data]
except IOError:
print['Error While Opening the file!']
Ví dụ dưới đây đọc tệp một byte tại một thời điểm và in byte.
[255 216 255 ... 179 255 217]
Nếu bạn muốn hiểu chi tiết, hãy đọc trên
Đọc từng dòng tệp nhị phân
Đọc tệp nhị phân đầy đủ trong một lần bắnread binary file line by line.
Python đọc tệp nhị phân và chuyển đổi thành ASCII
Đọc tệp nhị phân vào DataFrame
Đọc tiêu đề bỏ qua tệp nhị phân
Thí dụ
f = open["c:\temp\Binary_File.jpg",'rb']
lines = f.readlines[]
for line in lines:
print[line.rstrip[]]
Ví dụ dưới đây đọc tệp một byte tại một thời điểm và in byte.
import numpy as np
from matplotlib import pylab as plt
A = np.fromfile[filename, dtype='int16', sep=""]
A = A.reshape[[1024, 1024]]
plt.imshow[A]
0Đọc tệp nhị phân đầy đủ trong một lần bắn
Trong phần này, bạn sẽ học cách đọc tệp nhị phân trong một lần chụp.
Bạn có thể làm điều này bằng cách chuyển
b'\xd8'
b'\xff'
b'\xe0'
b'\x00'
b'\x10'
b'J'
b'F'
b'I'
b'F'
b'\x00'
b'\x01'
b'\x01'
b'\x00'
b'\x00'
b'\x01'
b'\x00'
b'\x01'
b'\x00'
b'\x00'
b'\xff'
b'\xed'
b'\x00'
b'|'
b'P'
b'h'
b'o'
b't'
b'o'
b's'
b'h'
b'o'
b'p'
b' '
b'3'
b'.'
b'0'
b'\xc6'
b'\xb3'
b'\xff'
b'\xd9'
b''
7 cho phương thức b'\xd8'
b'\xff'
b'\xe0'
b'\x00'
b'\x10'
b'J'
b'F'
b'I'
b'F'
b'\x00'
b'\x01'
b'\x01'
b'\x00'
b'\x00'
b'\x01'
b'\x00'
b'\x01'
b'\x00'
b'\x00'
b'\xff'
b'\xed'
b'\x00'
b'|'
b'P'
b'h'
b'o'
b't'
b'o'
b's'
b'h'
b'o'
b'p'
b' '
b'3'
b'.'
b'0'
b'\xc6'
b'\xb3'
b'\xff'
b'\xd9'
b''
8. Điều này sẽ đọc tệp nhị phân đầy đủ trong một lần chụp như hình dưới đây.Thí dụ
import numpy as np
from matplotlib import pylab as plt
A = np.fromfile[filename, dtype='int16', sep=""]
A = A.reshape[[1024, 1024]]
plt.imshow[A]
1Đầu ra
import numpy as np
from matplotlib import pylab as plt
A = np.fromfile[filename, dtype='int16', sep=""]
A = A.reshape[[1024, 1024]]
plt.imshow[A]
2Python đọc tệp nhị phân và chuyển đổi thành ASCII
Trong phần này, bạn sẽ học cách đọc một tệp nhị phân và chuyển đổi sang ASCII bằng thư viện Binascii. Điều này sẽ chuyển đổi tất cả các byte thành các ký tự ASCII.how to read a binary file and convert to ASCII using the binascii library. This will convert all the bytes into ASCII characters.
Đọc tệp là nhị phân như được giải thích trong phần trước.
Tiếp theo, sử dụng phương pháp
b'\xd8'
b'\xff'
b'\xe0'
b'\x00'
b'\x10'
b'J'
b'F'
b'I'
b'F'
b'\x00'
b'\x01'
b'\x01'
b'\x00'
b'\x00'
b'\x01'
b'\x00'
b'\x01'
b'\x00'
b'\x00'
b'\xff'
b'\xed'
b'\x00'
b'|'
b'P'
b'h'
b'o'
b't'
b'o'
b's'
b'h'
b'o'
b'p'
b' '
b'3'
b'.'
b'0'
b'\xc6'
b'\xb3'
b'\xff'
b'\xd9'
b''
9. Điều này sẽ chuyển đổi các byte thành try:
with open["c:\temp\Binary_File.jpg", "rb"] as f:
mybytearray = bytearray[]
# Do stuff with byte.
mybytearray+=f.read[1]
mybytearray+=f.read[1]
mybytearray+=f.read[1]
mybytearray+=f.read[1]
mybytearray+=f.read[1]
print[mybytearray]
except IOError:
print['Error While Opening the file!']
0 và trả về giá trị try:
with open["c:\temp\Binary_File.jpg", "rb"] as f:
mybytearray = bytearray[]
# Do stuff with byte.
mybytearray+=f.read[1]
mybytearray+=f.read[1]
mybytearray+=f.read[1]
mybytearray+=f.read[1]
mybytearray+=f.read[1]
print[mybytearray]
except IOError:
print['Error While Opening the file!']
0.convert the bytes into try:
with open["c:\temp\Binary_File.jpg", "rb"] as f:
mybytearray = bytearray[]
# Do stuff with byte.
mybytearray+=f.read[1]
mybytearray+=f.read[1]
mybytearray+=f.read[1]
mybytearray+=f.read[1]
mybytearray+=f.read[1]
print[mybytearray]
except IOError:
print['Error While Opening the file!']
0 and return an try:
with open["c:\temp\Binary_File.jpg", "rb"] as f:
mybytearray = bytearray[]
# Do stuff with byte.
mybytearray+=f.read[1]
mybytearray+=f.read[1]
mybytearray+=f.read[1]
mybytearray+=f.read[1]
mybytearray+=f.read[1]
print[mybytearray]
except IOError:
print['Error While Opening the file!']
0 value.Sau đó, bạn có thể in cái này để kiểm tra các ký tự
try:
with open["c:\temp\Binary_File.jpg", "rb"] as f:
mybytearray = bytearray[]
# Do stuff with byte.
mybytearray+=f.read[1]
mybytearray+=f.read[1]
mybytearray+=f.read[1]
mybytearray+=f.read[1]
mybytearray+=f.read[1]
print[mybytearray]
except IOError:
print['Error While Opening the file!']
0.Thí dụ
import numpy as np
from matplotlib import pylab as plt
A = np.fromfile[filename, dtype='int16', sep=""]
A = A.reshape[[1024, 1024]]
plt.imshow[A]
3Đầu ra
import numpy as np
from matplotlib import pylab as plt
A = np.fromfile[filename, dtype='int16', sep=""]
A = A.reshape[[1024, 1024]]
plt.imshow[A]
4Python đọc tệp nhị phân và chuyển đổi thành ASCII
Trong phần này, bạn sẽ học cách đọc một tệp nhị phân và chuyển đổi sang ASCII bằng thư viện Binascii. Điều này sẽ chuyển đổi tất cả các byte thành các ký tự ASCII.read the binary file into pandas dataframe.
Đọc tệp là nhị phân như được giải thích trong phần trước.no method available to read the binary file to
4 directly.try:
with open["c:\temp\Binary_File.jpg", "rb"] as f:
mybytearray = bytearray[]
# Do stuff with byte.
mybytearray+=f.read[1]
mybytearray+=f.read[1]
mybytearray+=f.read[1]
mybytearray+=f.read[1]
mybytearray+=f.read[1]
print[mybytearray]
except IOError:
print['Error While Opening the file!']
Tiếp theo, sử dụng phương pháp
b'\xd8'
b'\xff'
b'\xe0'
b'\x00'
b'\x10'
b'J'
b'F'
b'I'
b'F'
b'\x00'
b'\x01'
b'\x01'
b'\x00'
b'\x00'
b'\x01'
b'\x00'
b'\x01'
b'\x00'
b'\x00'
b'\xff'
b'\xed'
b'\x00'
b'|'
b'P'
b'h'
b'o'
b't'
b'o'
b's'
b'h'
b'o'
b'p'
b' '
b'3'
b'.'
b'0'
b'\xc6'
b'\xb3'
b'\xff'
b'\xd9'
b''
9. Điều này sẽ chuyển đổi các byte thành try:
with open["c:\temp\Binary_File.jpg", "rb"] as f:
mybytearray = bytearray[]
# Do stuff with byte.
mybytearray+=f.read[1]
mybytearray+=f.read[1]
mybytearray+=f.read[1]
mybytearray+=f.read[1]
mybytearray+=f.read[1]
print[mybytearray]
except IOError:
print['Error While Opening the file!']
0 và trả về giá trị try:
with open["c:\temp\Binary_File.jpg", "rb"] as f:
mybytearray = bytearray[]
# Do stuff with byte.
mybytearray+=f.read[1]
mybytearray+=f.read[1]
mybytearray+=f.read[1]
mybytearray+=f.read[1]
mybytearray+=f.read[1]
print[mybytearray]
except IOError:
print['Error While Opening the file!']
0.Sau đó, bạn có thể in cái này để kiểm tra các ký tự
try:
with open["c:\temp\Binary_File.jpg", "rb"] as f:
mybytearray = bytearray[]
# Do stuff with byte.
mybytearray+=f.read[1]
mybytearray+=f.read[1]
mybytearray+=f.read[1]
mybytearray+=f.read[1]
mybytearray+=f.read[1]
print[mybytearray]
except IOError:
print['Error While Opening the file!']
0.Thí dụ
import numpy as np
from matplotlib import pylab as plt
A = np.fromfile[filename, dtype='int16', sep=""]
A = A.reshape[[1024, 1024]]
plt.imshow[A]
5Đầu ra
import numpy as np
from matplotlib import pylab as plt
A = np.fromfile[filename, dtype='int16', sep=""]
A = A.reshape[[1024, 1024]]
plt.imshow[A]
6Python đọc tệp nhị phân và chuyển đổi thành ASCII
Trong phần này, bạn sẽ học cách đọc một tệp nhị phân và chuyển đổi sang ASCII bằng thư viện Binascii. Điều này sẽ chuyển đổi tất cả các byte thành các ký tự ASCII.
Đọc tệp là nhị phân như được giải thích trong phần trước.
Tiếp theo, sử dụng phương pháp
b'\xd8'
b'\xff'
b'\xe0'
b'\x00'
b'\x10'
b'J'
b'F'
b'I'
b'F'
b'\x00'
b'\x01'
b'\x01'
b'\x00'
b'\x00'
b'\x01'
b'\x00'
b'\x01'
b'\x00'
b'\x00'
b'\xff'
b'\xed'
b'\x00'
b'|'
b'P'
b'h'
b'o'
b't'
b'o'
b's'
b'h'
b'o'
b'p'
b' '
b'3'
b'.'
b'0'
b'\xc6'
b'\xb3'
b'\xff'
b'\xd9'
b''
9. Điều này sẽ chuyển đổi các byte thành try:
with open["c:\temp\Binary_File.jpg", "rb"] as f:
mybytearray = bytearray[]
# Do stuff with byte.
mybytearray+=f.read[1]
mybytearray+=f.read[1]
mybytearray+=f.read[1]
mybytearray+=f.read[1]
mybytearray+=f.read[1]
print[mybytearray]
except IOError:
print['Error While Opening the file!']
0 và trả về giá trị try:
with open["c:\temp\Binary_File.jpg", "rb"] as f:
mybytearray = bytearray[]
# Do stuff with byte.
mybytearray+=f.read[1]
mybytearray+=f.read[1]
mybytearray+=f.read[1]
mybytearray+=f.read[1]
mybytearray+=f.read[1]
print[mybytearray]
except IOError:
print['Error While Opening the file!']
0.read binary file, skipping the header line in the binary file. Some binary files will be having the ASCII header in them.Sau đó, bạn có thể in cái này để kiểm tra các ký tự
try:
with open["c:\temp\Binary_File.jpg", "rb"] as f:
mybytearray = bytearray[]
# Do stuff with byte.
mybytearray+=f.read[1]
mybytearray+=f.read[1]
mybytearray+=f.read[1]
mybytearray+=f.read[1]
mybytearray+=f.read[1]
print[mybytearray]
except IOError:
print['Error While Opening the file!']
0.Đọc tệp nhị phân vào DataFrame
Trong phần này, bạn sẽ học cách đọc tệp nhị phân vào Pandas DataFrame.line 0 will be ignored.
Thí dụ
import numpy as np
from matplotlib import pylab as plt
A = np.fromfile[filename, dtype='int16', sep=""]
A = A.reshape[[1024, 1024]]
plt.imshow[A]
7Đầu ra
import numpy as np
from matplotlib import pylab as plt
A = np.fromfile[filename, dtype='int16', sep=""]
A = A.reshape[[1024, 1024]]
plt.imshow[A]
8Python đọc tệp nhị phân và chuyển đổi thành ASCII
Trong phần này, bạn sẽ học cách đọc một tệp nhị phân và chuyển đổi sang ASCII bằng thư viện Binascii. Điều này sẽ chuyển đổi tất cả các byte thành các ký tự ASCII.
Đọc tệp là nhị phân như được giải thích trong phần trước.cannot be read in this mode. You may face problems while pickling a binary file. As invalid load key errors may occur.
Tiếp theo, sử dụng phương pháp
b'\xd8'
b'\xff'
b'\xe0'
b'\x00'
b'\x10'
b'J'
b'F'
b'I'
b'F'
b'\x00'
b'\x01'
b'\x01'
b'\x00'
b'\x00'
b'\x01'
b'\x00'
b'\x01'
b'\x00'
b'\x00'
b'\xff'
b'\xed'
b'\x00'
b'|'
b'P'
b'h'
b'o'
b't'
b'o'
b's'
b'h'
b'o'
b'p'
b' '
b'3'
b'.'
b'0'
b'\xc6'
b'\xb3'
b'\xff'
b'\xd9'
b''
9. Điều này sẽ chuyển đổi các byte thành try:
with open["c:\temp\Binary_File.jpg", "rb"] as f:
mybytearray = bytearray[]
# Do stuff with byte.
mybytearray+=f.read[1]
mybytearray+=f.read[1]
mybytearray+=f.read[1]
mybytearray+=f.read[1]
mybytearray+=f.read[1]
print[mybytearray]
except IOError:
print['Error While Opening the file!']
0 và trả về giá trị try:
with open["c:\temp\Binary_File.jpg", "rb"] as f:
mybytearray = bytearray[]
# Do stuff with byte.
mybytearray+=f.read[1]
mybytearray+=f.read[1]
mybytearray+=f.read[1]
mybytearray+=f.read[1]
mybytearray+=f.read[1]
print[mybytearray]
except IOError:
print['Error While Opening the file!']
0.not recommended to use this method.Thí dụ
import numpy as np
from matplotlib import pylab as plt
A = np.fromfile[filename, dtype='int16', sep=""]
A = A.reshape[[1024, 1024]]
plt.imshow[A]
9Đầu ra
try:
with open["c:\temp\Binary_File.jpg", "rb"] as f:
byte = f.read[1]
while byte:
# Do stuff with byte.
byte = f.read[1]
print[byte]
except IOError:
print['Error While Opening the file!']
0Sau đó, bạn có thể in cái này để kiểm tra các ký tự try:
with open["c:\temp\Binary_File.jpg", "rb"] as f:
mybytearray = bytearray[]
# Do stuff with byte.
mybytearray+=f.read[1]
mybytearray+=f.read[1]
mybytearray+=f.read[1]
mybytearray+=f.read[1]
mybytearray+=f.read[1]
print[mybytearray]
except IOError:
print['Error While Opening the file!']
0.
try:
with open["c:\temp\Binary_File.jpg", "rb"] as f:
mybytearray = bytearray[]
# Do stuff with byte.
mybytearray+=f.read[1]
mybytearray+=f.read[1]
mybytearray+=f.read[1]
mybytearray+=f.read[1]
mybytearray+=f.read[1]
print[mybytearray]
except IOError:
print['Error While Opening the file!']
Đọc tệp nhị phân vào DataFrame
Trong phần này, bạn sẽ học cách đọc tệp nhị phân vào Pandas DataFrame.
Đầu tiên, bạn cần đọc tệp nhị phân vào
try:
with open["c:\temp\Binary_File.jpg", "rb"] as f:
mybytearray = bytearray[]
# Do stuff with byte.
mybytearray+=f.read[1]
mybytearray+=f.read[1]
mybytearray+=f.read[1]
mybytearray+=f.read[1]
mybytearray+=f.read[1]
print[mybytearray]
except IOError:
print['Error While Opening the file!']
3. Bởi vì không có phương pháp nào để đọc trực tiếp tệp nhị phân để try:
with open["c:\temp\Binary_File.jpg", "rb"] as f:
mybytearray = bytearray[]
# Do stuff with byte.
mybytearray+=f.read[1]
mybytearray+=f.read[1]
mybytearray+=f.read[1]
mybytearray+=f.read[1]
mybytearray+=f.read[1]
print[mybytearray]
except IOError:
print['Error While Opening the file!']
4.