问题描述
在Windows上跑一段代码时,遇到如下问题:
KeyError: "There is no item named 'CDR_Data\\\\CDR.Corpus.v010516\\\\CDR_DevelopmentSet.PubTator.txt' in the archive"
原因分析
这是一段Python代码,代码中使用到了zipfile库,它首先创建了一个ZipFile对象,然后在调用read()函数时弹出了错误。
相关语句如下:
def download_zip(url: str) -> ZipFile: r = requests.get(url) z = ZipFile(io.BytesIO(r.content)) return z
def _download_corpus() -> Tuple[str, str, str]: z = util.download_zip(CDR_URL) train = z.read(str(Path(PARENT_DIR) / TRAIN_FILENAME)).decode() valid = z.read(str(Path(PARENT_DIR) / VALID_FILENAME)).decode() test = z.read(str(Path(PARENT_DIR) / TEST_FILENAME)).decode() return train, valid, test
上述代码中,文件路径是通过Path()函数进行拼接,所生成的路径是由“\”分隔的,比如:
CDR_Data\CDR.Corpus.v010516\CDR_TrainingSet.PubTator.txt
可能是由于在不同平台上的编码格式有差异导致这在Windows上识别出错。
解决方法
弃用原有的路径拼接函数Path(),路径直接改成‘/’拼接,比如:
def _download_corpus() -> Tuple[str, str, str]: z = util.download_zip(CDR_URL) train = z.read('CDR_Data/CDR.Corpus.v010516/CDR_TrainingSet.PubTator.txt').decode() valid = z.read('CDR_Data/CDR.Corpus.v010516/CDR_DevelopmentSet.PubTator.txt').decode() test = z.read('CDR_Data/CDR.Corpus.v010516/CDR_TestSet.PubTator.txt').decode() return train, valid, test
标签:named,xxx,CDR,read,There,decode,str,download,Path From: https://www.cnblogs.com/engpj/p/17968409