相关疑难解决方法(0)

将dict的键和值从`unicode`转换为`str`的​​最快方法?

我从一个代码"层"接收一个字典,在将代码传递到另一个"层"之前执行一些计算/修改.原始字典的键和"字符串"值是unicode,但它们传递的层只接受str.

这将经常被调用,所以我想知道什么是最快的方式来转换像:

{ u'spam': u'eggs', u'foo': True, u'bar': { u'baz': 97 } }
Run Code Online (Sandbox Code Playgroud)

...至:

{ 'spam': 'eggs', 'foo': True, 'bar': { 'baz': 97 } }
Run Code Online (Sandbox Code Playgroud)

...请记住,非"字符串"值需要保留为原始类型.

有什么想法吗?

python types casting

77
推荐指数
3
解决办法
10万
查看次数

Python 如何将一个zip中的文件复制到内存中的另一个zip中?

目的

将 zip 存档拆分为较小的 zip 存档,每个新 zip 均匀分布文件数。

例子

源 zip(100 个文件)

  • src/100-Test.zip

目标 zip(每个 25 个文件):

  • 目的地/1.zip
  • 目的地/2.zip
  • 目的地/3.zip
  • 目的地/4.zip

描述

因此,我能够打开 zip 文件并迭代内容以将它们拆分,但我无法写入该文件。因为我没有对 zip 内容做任何事情,所以我认为我不需要做任何 StringIO 的东西或任何东西?

代码

zipFileNameSrc = '100-Test.zip'
zipFile = open(zipFileNameSrc)
unzippedFile = zipfile.ZipFile(zipFile)
imgList = [(s, unzippedFile.read(s)) for s in unzippedFile.namelist() if (".jpg" or ".JPG") in s]
#image names: imgList[i][0]  and  images: imgList[i][1]

#...
#...additional logic to split into sets of 25 images
#...fileTuplesList = imgList[:25]
zipNo = 1
#zipFileDest = destination + "/" …
Run Code Online (Sandbox Code Playgroud)

python zip

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

标签 统计

python ×2

casting ×1

types ×1

zip ×1