我正在尝试从一组唯一值构建一个dict作为键和一个压缩的元组列表来提供项目.
set = ("a","b","c")
lst 1 =("a","a","b","b","c","d","d")
lst 2 =(1,2,3,3,4,5,6,)
zip = [("a",1),("a",2),("b",3),("b",3),("c",4),("d",5)("d",6)
dct = {"a":1,2 "b":3,3 "c":4 "d":5,6}
Run Code Online (Sandbox Code Playgroud)
但我得到:
dct = {"a":1,"b":3,"c":4,"d":5}
Run Code Online (Sandbox Code Playgroud)
这是我目前的代码:
#make two lists
rtList = ["EVT","EVT","EVT","EVT","EVT","EVT","EVT","HIL"]
raList = ["C64G","C64R","C64O","C32G","C96G","C96R","C96O","RA96O"]
# make a set of unique codes in the first list
routes = set()
for r in rtList:
routes.add(r)
#zip the lists
RtRaList = zip(rtList,raList)
#print RtRaList
# make a dictionary with list one as the keys and list two as the values
SrvCodeDct = {}
for …Run Code Online (Sandbox Code Playgroud) 有文件夹路径:
P:\\2018\\Archive\\
Run Code Online (Sandbox Code Playgroud)
我想以编程方式创建许多 zipfile,但我从测试开始。我将这个测试 zip 文件命名为“CO_007_II.zip”,并将尝试在上述位置创建:
import zipfile as zp
with zp.ZipFile("P:\\2018\\Archive\\CO_007_II.zip",'w') as myzip:
myzip.write(r"P:\2018\CO_007_II")
Run Code Online (Sandbox Code Playgroud)
但我得到错误!
...
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
File "C:\Python27\ArcGIS10.2\lib\zipfile.py", line 752, in __init__
self.fp = open(file, modeDict[mode])
IOError: [Errno 2] No such file or directory: 'P:\\2018\\Archive\\CO_007_II.zip'
Run Code Online (Sandbox Code Playgroud)
这不是创建新 zipfile 的方法吗?我知道文件不存在。是为什么我使用“w”模式,不是吗?
这是文档:
https://docs.python.org/3/library/zipfile.html
它说:
'w' 截断并写入一个新文件
文档页面上的示例:
with ZipFile('spam.zip', 'w') as myzip:
myzip.write('eggs.txt')
Run Code Online (Sandbox Code Playgroud)
代码在两天前工作以创建新的 zip 文件,但没有添加文件夹。今天没有任何效果!为什么不?所有路径都有效。如何使用 python 创建新的 zip 文件并向其中添加文件夹?