def get_data(json_content): imgpath = json_content.get('imagePath') data.append(imgpath) flags = json_content.get('flags') values = list(flags. values()) label = 0 for i in range(len( values)): if values [i]: label= i+1 data = [] data.append(imgpath) data.append(label) return data temp_json=read_json(r'D:\data_1\flowers\1185.json') print(get_data(temp_json))
时间: 2024-02-20 09:58:42 浏览: 232
在这段代码中,第6行的`data`列表在函数中被重复初始化了。你需要把第6行移到第3行之前,以确保`data`列表只被初始化一次。此外,在第9行中,你应该使用`values()`方法而不是`values.`。修改后的代码应该是这样的:
```python
def get_data(json_content):
data = []
imgpath = json_content.get('imagePath')
data.append(imgpath)
flags = json_content.get('flags')
values = list(flags.values())
label = 0
for i in range(len(values)):
if values[i]:
label = i + 1
data.append(label)
return data
temp_json = read_json(r'D:\data_1\flowers\1185.json')
print(get_data(temp_json))
```
这个函数将返回一个包含图像路径和标签的列表。在这个例子中,我们打印了返回的数据,以确保函数正常工作。
相关问题
import requests import re import os from bs4 import BeautifulSoup from scrapy import Spider from PIL import Image import io def GetBasicInfo(url): res = requests.get(url, headers=headers) res.encoding = 'utf-8' soup = BeautifulSoup(res.text, 'lxml') tmp = soup.find(attrs={'class': 'mhlistbody'}) chapters = tmp.ul.contents chapters.reverse() return chapters def GetRealUrls(mh_info): imgs = [] comic_size = re.findall(r'comic_size:"(.*?)"', mh_info)[0] base_url = 'https://mhpic.jumanhua.com/comic/{}.jpg%s.webp' % comic_size num_img = int(re.findall(r'totalimg:(\d+)', mh_info)[0]) pageid = int(re.findall(r'pageid:(\d+)', mh_info)[0]) imgpath = re.findall(r'imgpath:"(.*?)"', mh_info)[0] start = 0 while True: idx = imgpath.find('\\', start) if idx == -1: break imgpath = imgpath[:idx] + imgpath[idx+1:] start = idx + 1 for i in range(num_img): realpath = str() for s in imgpath: realpath += chr(ord(s) - pageid % 10) url = base_url.format(realpath + str(i+1)) imgs.append([url, str(i+1)+'.jpg']) return imgs def DownloadChapter(savepath, url): if not os.path.exists(savepath): os.mkdir(savepath) res = requests.get(url, headers=headers) res.encoding = 'utf-8' mh_info = re.findall(r'mh_info={(.*?)}', res.text)[0] img_urls = GetRealUrls(mh_info) for img_url in img_urls: img_content = requests.get(img_url[0]).content filename = os.path.join(savepath, img_url[1]) img = Image.open(io.BytesIO(img_content)) img.save(filename) if __name__ == '__main__': url = 'https://www.manhuatai.com/yaoshenji/' headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.82 Safari/537.36'} savepath = url.split('/')[-2] Spider(url,savepath)
这段代码是一个简单的Python脚本,用于下载指定漫画网站的漫画图片。它使用了`requests`库来发送HTTP请求,`re`库用于正则表达式匹配,`os`库用于处理文件路径,`BeautifulSoup`库用于解析HTML,`PIL`库用于处理图片,`io`库用于读取图片内容。
具体来说,脚本中的`GetBasicInfo`函数用于获取漫画的章节信息,`GetRealUrls`函数用于获取漫画章节中每页图片的真实URL,`DownloadChapter`函数用于下载指定章节的漫画图片。
在脚本的主程序中,设置了要下载的漫画网址和请求头信息,然后调用`Spider`函数来下载漫画。
需要注意的是,该脚本使用了第三方库和一些特定的网站结构,如果要使用该脚本下载其他网站的漫画,可能需要进行适当的修改。
希望以上解答能够帮到您!如果还有其他问题,请随时提问。
import gzip import os import pickle import numpy as np def load_mnist(path, kind='train'): labels_path = os.path.join(path, '%s-labels-idx1-ubyte.gz' % kind) images_path = os.path.join(path, '%s-images-idx3-ubyte.gz' % kind) with gzip.open(labels_path, 'rb') as lbpath: labels = np.frombuffer(lbpath.read(), dtype=np.uint8, offset=8) with gzip.open(images_path, 'rb') as imgpath: images = np.frombuffer(imgpath.read(), dtype=np.uint8, offset=16).reshape(len(labels), 784) return images, labels def get_mnist_data(num_training=5000, num_validation=500, num_test=500): mnist_dir = r'D:\daima\mnist' # 修改为mnist数据集所在的目录 X_train, y_train = load_mnist(mnist_dir, kind='train') X_test, y_test = load_mnist(mnist_dir, kind='t10k') print(X_train.shape) mask = range(num_training, num_training + num_validation) X_val = X_train[mask] y_val = y_train[mask] mask = range(num_training) X_train = X_train[mask] y_train = y_train[mask] mask = range(num_test) X_test = X_test[mask] y_test = y_test[mask] X_train = X_train.astype('float32') / 255 X_val = X_val.astype('float32') / 255 X_test = X_test.astype('float32') / 255 return X_train, y_train, X_val, y_val, X_test, y_testTypeError: tuple indices must be integers or slices, not str
这段代码出现了一个错误:TypeError: tuple indices must be integers or slices, not str。这是因为在load_mnist函数中返回的是一个元组类型的变量(images, labels),而在get_mnist_data函数中却使用了类似字典的方式访问这个元组变量,如X_train, y_train = load_mnist(mnist_dir, kind='train'),应该改为X_train, y_train = load_mnist(mnist_dir, kind=0),X_test, y_test = load_mnist(mnist_dir, kind=1),这样就能够正确访问这个元组变量的元素了。
阅读全文