- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
因此,我尝试迭代 tar 中的多个文件,然后将该数据加载到一些 ctype structures's I've defined 中。 。这对于非 tar 文件工作得很好,但后来我发现 ExFileObject tarfile 的 extractfile(member)
方法返回的内容不支持 .readinto(b)
方法。
所以现在这就是我正在做的事情:
import os
import tarfile
import io
from ctypes import c_uint, c_char, c_ubyte, c_ushort, BigEndianStructure
class MyStructure(BigEndianStructure):
_pack_ = True
_fields_ = [
("id", c_uint), # 4 bytes
("namefield", c_char * 32), # 32 bytes
("timestamp", c_ubyte * 4), # 4 bytes
("payload_length", c_ushort), # 2 bytes
]
def process_tar(tar_files):
"""
untar and return file objects to be parsed
"""
for filepath in tar_files:
f = os.path.abspath(filepath)
with tarfile.open(f, 'r:*') as tar_f:
#tar_f.fileobject = io.BufferedReader
for tarinfo_member in tar_f.getmembers():
if tarinfo_member.isfile():
yield tar_f.extractfile(tarinfo_member)
f = "somefiles.tar.gz"
for tar_member_fileobj in process_tar([f]):
mystruct = MyStructure()
tar_member_fileobj.readinto(mystruct)
得到这个:
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-4-257ee4b46c31> in <module>()
29 for tar_member_fileobj in process_tar([f]):
30 mystruct = MyStructure()
---> 31 tar_member_fileobj.readinto(mystruct)
AttributeError: 'ExFileObject' object has no attribute 'readinto'
有没有办法可以将此方法添加到 ExFileObject 中?或者,是否有另一种方法可以轻松地将我的数据加载到我定义的 ctypes 结构中?我注意到在 tarfile
对象中,您似乎可以将 fileobject
设置为用于返回的 tarinfo 文件,但仅交换 io.BufferedReader 似乎不起作用。
(我尝试将 ExFileObject 读入 StringIO,但它似乎也没有正确实现 readinto()
...我想我可以 extractall()
到内存中的文件空间并将文件作为标准文件对象重新打开,但我想避免这种情况,因为这样我就需要额外的配置)
最佳答案
ExFileObject
无法使用 readinto
方法,但您仍然可以通过使用 read
读取文件头并复制使用 memmove
将数据放入结构中:
memmove(byref(mystruct),
tar_member_fileobj.read(sizeof(mystruct)),
sizeof(mystruct)
)
例如:
import os
import tarfile
import io
from ctypes import *
class MyStructure(BigEndianStructure):
_pack_ = True
_fields_ = [
("id", c_uint), # 4 bytes
("namefield", c_char * 32), # 32 bytes
("timestamp", c_ubyte * 4), # 4 bytes
("payload_length", c_ushort), # 2 bytes
]
def process_tar(tar_files):
"""
untar and return file objects to be parsed
"""
for filepath in tar_files:
f = os.path.abspath(filepath)
with tarfile.open(f, 'r:*') as tar_f:
#tar_f.fileobject = io.BufferedReader
for tarinfo_member in tar_f.getmembers():
if tarinfo_member.isfile():
yield tar_f.extractfile(tarinfo_member)
# test tar.gz file
open('somefiles.tar.gz', 'wb').write(
'\x1f\x8b\x08\x08\xad\x5c\x34\x52\x02\x00\x66\x6f\x6f\x2e\x74\
\x61\x72\x00\xed\xca\x49\x0a\x83\x40\x14\x04\xd0\x7f\x85\xdc\
\xa0\x43\x92\x6d\xf8\xdd\xa6\xed\x6d\xe6\xc9\x79\x1e\x76\x82\
\x0a\x82\x4b\xef\x8f\xf6\x42\xf0\x02\x6e\xe4\xbf\x4d\x41\x55\
\xb5\x5d\xdf\x9c\xeb\x6a\xa8\x60\x3d\x1c\x51\x29\xc5\x80\x69\
\xb8\xc8\x99\x14\x8c\x0b\xc1\x25\x4a\x53\x5c\x4c\x5d\x28\x9c\
\xfe\x08\x64\x6d\xbb\xc9\xed\xfe\x78\xbe\xde\x9f\xef\xef\x6f\
\xd9\x8e\xeb\xf9\x41\x18\xc5\x49\x9a\xe5\x45\xb9\xbf\x1e\x8e\
\x27\xd0\xbb\x61\x00\x21\x84\x90\x0d\x19\x01\xd4\xe8\x88\xcf\
\x00\x08\x00\x00'
)
f = "somefiles.tar.gz"
for tar_member_fileobj in process_tar([f]):
mystruct = MyStructure()
memmove(byref(mystruct),
tar_member_fileobj.read(sizeof(mystruct)),
sizeof(mystruct)
)
print hex(mystruct.id)
print mystruct.namefield
print ''.join(map(chr, mystruct.timestamp))
print hex(mystruct.payload_length)
字符串 \x1f\x8b\x08\x08\xad ...
是一个 tar.gz 文件,包含:
\x11\x11\x11\x11ABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%\x00ABCD33
将该数据复制到 mystruct
后,它应该打印:
0x11111111L
ABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%
ABCD
0x3333
关于python - 将 .readinto(b) 方法添加到 tarfile 的 ExFileObject 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18791003/
我想了解 Ruby 方法 methods() 是如何工作的。 我尝试使用“ruby 方法”在 Google 上搜索,但这不是我需要的。 我也看过 ruby-doc.org,但我没有找到这种方法。
Test 方法 对指定的字符串执行一个正则表达式搜索,并返回一个 Boolean 值指示是否找到匹配的模式。 object.Test(string) 参数 object 必选项。总是一个
Replace 方法 替换在正则表达式查找中找到的文本。 object.Replace(string1, string2) 参数 object 必选项。总是一个 RegExp 对象的名称。
Raise 方法 生成运行时错误 object.Raise(number, source, description, helpfile, helpcontext) 参数 object 应为
Execute 方法 对指定的字符串执行正则表达式搜索。 object.Execute(string) 参数 object 必选项。总是一个 RegExp 对象的名称。 string
Clear 方法 清除 Err 对象的所有属性设置。 object.Clear object 应为 Err 对象的名称。 说明 在错误处理后,使用 Clear 显式地清除 Err 对象。此
CopyFile 方法 将一个或多个文件从某位置复制到另一位置。 object.CopyFile source, destination[, overwrite] 参数 object 必选
Copy 方法 将指定的文件或文件夹从某位置复制到另一位置。 object.Copy destination[, overwrite] 参数 object 必选项。应为 File 或 F
Close 方法 关闭打开的 TextStream 文件。 object.Close object 应为 TextStream 对象的名称。 说明 下面例子举例说明如何使用 Close 方
BuildPath 方法 向现有路径后添加名称。 object.BuildPath(path, name) 参数 object 必选项。应为 FileSystemObject 对象的名称
GetFolder 方法 返回与指定的路径中某文件夹相应的 Folder 对象。 object.GetFolder(folderspec) 参数 object 必选项。应为 FileSy
GetFileName 方法 返回指定路径(不是指定驱动器路径部分)的最后一个文件或文件夹。 object.GetFileName(pathspec) 参数 object 必选项。应为
GetFile 方法 返回与指定路径中某文件相应的 File 对象。 object.GetFile(filespec) 参数 object 必选项。应为 FileSystemObject
GetExtensionName 方法 返回字符串,该字符串包含路径最后一个组成部分的扩展名。 object.GetExtensionName(path) 参数 object 必选项。应
GetDriveName 方法 返回包含指定路径中驱动器名的字符串。 object.GetDriveName(path) 参数 object 必选项。应为 FileSystemObjec
GetDrive 方法 返回与指定的路径中驱动器相对应的 Drive 对象。 object.GetDrive drivespec 参数 object 必选项。应为 FileSystemO
GetBaseName 方法 返回字符串,其中包含文件的基本名 (不带扩展名), 或者提供的路径说明中的文件夹。 object.GetBaseName(path) 参数 object 必
GetAbsolutePathName 方法 从提供的指定路径中返回完整且含义明确的路径。 object.GetAbsolutePathName(pathspec) 参数 object
FolderExists 方法 如果指定的文件夹存在,则返回 True;否则返回 False。 object.FolderExists(folderspec) 参数 object 必选项
FileExists 方法 如果指定的文件存在返回 True;否则返回 False。 object.FileExists(filespec) 参数 object 必选项。应为 FileS
我是一名优秀的程序员,十分优秀!