我想将列表的内容写入文件。该列表包含从google api地方照片请求的每张照片的内容。当我尝试将列表的内容写入文件时,所有文件最终都带有同一张照片
这是我尝试写入文件的代码 任何人都可以帮助
from flask import render_template
import urllib.parse
import requests
import geocoder
import json
import imghdr
import io
import os
@app.route("/NearbyShops")
def NearbyShops():
final_url = nearby_search_url + urllib.parse.urlencode({'location':'34.015353,-6.830001',
'radius':'1500',
'type':'restaurant',
'key':''})
places_req = requests.get(final_url)
places_req.text
results = json.loads(places_req.text)
list_photo_reference = []
list_name = []
new_list_name= []
list_requests = []
lent_list_rq=len(list_requests)
lent = len(results)
for i in range(lent):
for ph in results["results"][i]["photos"]:
list_photo_reference.append(ph["photo_reference"])
for n in results["results"]:
list_name.append(n["name"])
for ref in list_photo_reference:
photo_final_url = photos_url + urllib.parse.urlencode({'maxwidth' :'350',
'photoreference' :ref,
'key':''})
photos_req = requests.get(photo_final_url)
list_requests.append(photos_req.content)
for rq,name in zip(list_requests,list_name):
photo_name= name.replace(" ","_") + "." + imghdr.what("", rq)
photo_dir = "App/static/photos/" + photo_name
with open(photo_dir, "wb+") as f:
f.write(rq)
f.close()
image_name = os.listdir('App/static/photos')
return render_template('NearbyShops.html', title='Nearby Shops',image_name=image_name)
答案 0 :(得分:0)
您的代码说:
对于每个请求(或任何
ValueError Traceback (most recent call last) <ipython-input-1-1e9eee4e6961> in <module> 17 from object_detection.utils import ops as utils_ops 18 ---> 19 if StrictVersion(tf.__version__) < StrictVersion('1.9.0'): 20 raise ImportError('Please upgrade your TensorFlow installation to v1.9.* or later!') ~/anaconda3/envs/tensorflow_cpu/lib/python3.7/distutils/version.py in __init__(self, vstring) 38 def __init__ (self, vstring=None): 39 if vstring: ---> 40 self.parse(vstring) 41 42 def __repr__ (self): ~/anaconda3/envs/tensorflow_cpu/lib/python3.7/distutils/version.py in parse(self, vstring) 135 match = self.version_re.match(vstring) 136 if not match: --> 137 raise ValueError("invalid version number '%s'" % vstring) 138 139 (major, minor, patch, prerelease, prerelease_num) = \ ValueError: invalid version number '1.13.0-rc1'
),请将所述请求写入rq
中的每个文件。
因此,计算机完全可以做到这一点,最终您将相同的请求/照片写入所有这些文件。
您可能想遍历list_name
对:
(photo, file)
这要求for rq, name in zip(list_requests, list_name):
photo_name = ...
...
和list_requests
的长度相等。