我正在阅读特定的holter数据(.ecg文件),并将用户指定的特定数据复制到新文件中。基本上,我只想将数据读入必要的内存。有没有办法使用.seek()只从(开始,结束)读取数据字节?
start = args.packet_start
end = args.packet_end
try:
print("Beginning copying of holter data...")
# Output the specific holter data
output_file = open("copied_holter.ecg", 'w')
# Read part of holter file into memory
holter = open(args.filename, 'rb')
data = holter.seek()
# Parse through holter data and copy to file
for index in range(start, end+1):
data_list = data[index]
output_file.write(data_list)
# Close the file streams
holter.close()
output_file.close()
except Exception as e:
print(e)
print("Exiting program, due to exception.")
exit(1)
print "Finished data copying operations!"
答案 0 :(得分:4)
seek
会将指针移动到指定位置,而不是返回数据。 read
willy返回指定的字节数。此外,在处理文件或类似文件的文件时,您应该使用with
语句。
with open(args.filename, 'rb') as holter, open("copied_holter.ecg", 'w') as output_file:
holter.seek(start)
data = holter.read(end-start)
output_file.write(data)