我正在尝试导入CSV文件作为Pandas数据框架。这些文件包含在一个较大的Zip文件中。下面是我的代码:
import tensorflow as tf
from tensorflow import keras
import pandas as pd
import zipfile
DataURL = "file:///c:/Users/MMoynihan/Desktop/TensorFlow/DNN_Dataset/SIM_IMGS.zip"
TagURL = "file:///c:/Users/MMoynihan/Desktop/TensorFlow/DNN_Dataset/SIM_TAGS.zip"
TagName1 = 'Sim_TAG0_2021-08-13-18-52.csv'
data_for_processing = keras.utils.get_file('plots', DataURL)
tags_for_processing = keras.utils.get_file('tags', DataURL)
zf = zipfile.ZipFile(tags_for_processing)
df = pd.read_csv(zf.open(TagName1))
无论出于何种原因,标记的文件没有被列为较大的Zip文件的一部分。
raise KeyError(
KeyError: "There is no item named 'Sim_TAG0_2021-08-13-18-52.csv' in the archive"
我测试了4或5个我知道存在的不同文件名,但这些都没有出现在zip中。
= = = =
我在访问图像文件时遇到了类似的问题。
import tensorflow as tf
from tensorflow import keras
import pandas as pd
import zipfile
DataURL = "file:///c:/Users/MMoynihan/Desktop/TensorFlow/DNN_Dataset/SIM_IMGS.zip"
TagURL = "file:///c:/Users/MMoynihan/Desktop/TensorFlow/DNN_Dataset/SIM_TAGS.zip"
TagTest = 'Sim_TAG7_2021-08-13-18-52.csv'
ImageTest = 'Sim_Image1_2021-08-13-18-52.png'
IMG_processing = keras.utils.get_file('plots', DataURL)
TAGS_processing = keras.utils.get_file('tags', DataURL)
archive = zipfile.ZipFile(IMG_processing, 'r')
imgdata = archive.read(ImageTest)
错误如下:
Raise KeyError(KeyError: "There is no item named 'Sim_Image1_2021-08-13-18-52.png' in the archive"
我解决了这个问题。文件名必须包含它所在的zip文件夹。在本例中:
TagTest = 'SIM_TAGS/Sim_TAG7_2021-08-13-18-52.csv'
ImageTest = 'SIM_IMGS`/Sim_Image1_2021-08-13-18-52.png'