我从一个代码"层"接收一个字典,在将代码传递到另一个"层"之前执行一些计算/修改.原始字典的键和"字符串"值是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)
...请记住,非"字符串"值需要保留为原始类型.
有什么想法吗?
目的
将 zip 存档拆分为较小的 zip 存档,每个新 zip 均匀分布文件数。
例子
源 zip(100 个文件)
目标 zip(每个 25 个文件):
描述
因此,我能够打开 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)