小编Har*_*thi的帖子

如何将字节类型转换为字典?

我有一个字节类型的对象,像这样:

b"{'one': 1, 'two': 2}"
Run Code Online (Sandbox Code Playgroud)

我需要使用python代码从字典中获取字典。我将其转换为字符串,然后如下转换为字典。

string = dictn.decode("utf-8")
print(type(string))
>> <class 'str'>
d = dict(toks.split(":") for toks in string.split(",") if toks)
Run Code Online (Sandbox Code Playgroud)

但是我收到以下错误:

------> d = dict(toks.split(":") for toks in string.split(",") if toks)
TypeError: 'bytes' object is not callable
Run Code Online (Sandbox Code Playgroud)

python string dictionary

8
推荐指数
4
解决办法
1万
查看次数

需要帮助使用 python 读取 tar 文件中的图像文件

我有一个 tar 文件,其中包含许多文件夹。每个文件夹中都有许多图像文件。我需要编写一个 python 脚本,它将读取每个图像文件并对图像执行一些操作(例如:阈值处理等),并将图像文件保存在我指定的目录中。此过程需要在不解压 tar 文件的情况下完成。

t = tarfile.open('example.tar', 'r')
for member in t.getmembers():
    f = t.extractfile(member)
Run Code Online (Sandbox Code Playgroud)

当我尝试打印时f,它正在返回None类型。我究竟做错了什么?

python image tarfile

5
推荐指数
1
解决办法
1797
查看次数

标签 统计

python ×2

dictionary ×1

image ×1

string ×1

tarfile ×1