在Python 2.7中使用多个变量的for循环。
你好,
我不确定如何处理这个问题,我有一个函数可以访问网站并下载一个.csv文件。它以特定格式保存.csv文件:name_uniqueID_dataType.csv。以下是代码:
import requests
name = "name1"
id = "id1"
dataType = "type1"
def downloadData():
URL = "http://www.website.com/data/%s" %name #downloads the file from the website. The last part of the URL is the name
r = requests.get(URL)
with open("data/%s_%s_%s.csv" %(name, id, dataType), "wb") as code: #create the file in the format name_id_dataType
code.write(r.content)
downloadData()
这段代码可以成功下载并保存文件。我希望能够对该函数运行for循环,每次使用三个变量。这些变量将被写成列表。
name = ["name1", "name2"]
id = ["id1", "id2"]
dataType = ["type1", "type2"]
每个列表中将列出100多个不同的项目,每个变量中有相同数量的项目。是否有办法在Python 2.7中使用for循环来完成这个任务?我已经研究了一整天,但是找不到解决方法。请注意,我是Python的新手,这是我的第一个问题。任何帮助或指导都将不胜感激。