在for-in循环中添加一个暂停

时间:2017-10-12 14:46:41

标签: python pandas csv for-in-loop

我有一个访问网站超过100次的脚本,并希望在每个项目之间添加一个小延迟。此外,如果可能,可以添加倒计时或处理栏,显示剩余的状态或项目数。代码示例如下;

import pandas as pd

urls = ['https://vpic.nhtsa.dot.gov/api/vehicles/GetModelsForMakeIdYear/makeId/440/vehicletype/car?format=csv', 
    'https://vpic.nhtsa.dot.gov/api/vehicles/GetModelsForMakeIdYear/makeId/441/vehicletype/car?format=csv', 
    'https://vpic.nhtsa.dot.gov/api/vehicles/GetModelsForMakeIdYear/makeId/442/vehicletype/car?format=csv', 
    'https://vpic.nhtsa.dot.gov/api/vehicles/GetModelsForMakeIdYear/makeId/443/vehicletype/car?format=csv', 
    'https://vpic.nhtsa.dot.gov/api/vehicles/GetModelsForMakeIdYear/makeId/445/vehicletype/car?format=csv', 
    'https://vpic.nhtsa.dot.gov/api/vehicles/GetModelsForMakeIdYear/makeId/448/vehicletype/car?format=csv']             


dfs = [pd.read_csv(url) for url in urls]
df = pd.concat(dfs, ignore_index=True)                                                                                      
df.to_csv('foo.csv')                                                                                                        

1 个答案:

答案 0 :(得分:3)

import time 
dfs = []
num = len(urls)
for i, url in enumerate(urls):
  dfs.append(pd.read_csv(url))
  print 'Process {}, {} left'.format(i+1, num-i-1)
  time.sleep(2)