2016-02-12 79 views
0

使用此代碼,我可以通過csv中的第一行來觸發它併發布內容。沒有for循環,它也很好。我也使用簡單的打印語句,能夠打印出csv中的所有行。我陷入困境的是如何通過我的csv(2300行)循環並替換兩個內聯變量。我已經嘗試了幾次迭代,周圍的聲明等,這是我最近的嘗試。Python:for循環在從csv中讀取時停在第一行

from __future__ import print_function 
import arcrest 
import json 
import csv 
if __name__ == "__main__": 
    username = "uid" 
    password = "pwd" 
    portalId = "id" 
    url = "http://www.arcgis.com/" 
    thumbnail_url = "" 
with open('TILES.csv') as csvfile: 
    inputFile = csv.DictReader(csvfile) 
    x = 0 # counter to display file count 
    for row in inputFile: 
     if x == 0: 
      map_json = { 
    "operationalLayers": [ 
    { 
     "templateUrl": "https://{subDomain}.tiles.mapbox.com/v4/abc.GRSM_"+row['ID']+"_pink/{level}/{col}/{row}.png?access_token=pk.secret", 
     "id": "GRSM_SPECIES_OBSERVATIONS_MAXENT_5733", 
     "type": "WebTiledLayer", 
     "layerType": "WebTiledLayer", 
     "title": row['Species']+" Prediction", 
     "copyright": "GRSM", 
     "fullExtent": { 
     "xmin": -20037508.342787, 
     "ymin": -20037508.34278, 
     "xmax": 20037508.34278, 
     "ymax": 20037508.342787, 
     "spatialReference": { 
      "wkid": 102100 
     } 
     }, 
     "subDomains": [ 
     "a", 
     "b", 
     "c", 
     "d" 
     ], 
     "visibility": True, 
     "opacity": 1 
    } 
    ], 
    "baseMap": { 
    "baseMapLayers": [ 
     { 
     "id": "defaultBasemap", 
     "layerType": "ArcGISTiledMapServiceLayer", 
     "opacity": 1, 
     "visibility": True, 
     "url": "http://services.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer" 
     } 
    ], 
    "title": "Topographic" 
    }, 
    "spatialReference": { 
    "wkid": 102100, 
    "latestWkid": 3857 
    }, 
    "version": "2.0" 
} 
    securityHandler = arcrest.AGOLTokenSecurityHandler(username, 
                 password) 
    # Create the administration connection 
    # 
    admin = arcrest.manageorg.Administration(url, securityHandler) 
    # Access the content properties to add the item 
    # 
    content = admin.content 
    # Get the user # 
    user = content.users.user() 
    # Provide the item parameters 
    # 
    itemParams = arcrest.manageorg.ItemParameter() 
    itemParams.title = "GRSM_"+row['Species'] 
    itemParams.thumbnailurl = "" 
    itemParams.type = "Web Map" 
    itemParams.snippet = "Maxent Output: "+row['Species'] 
    itemParams.licenseInfo = "License" 
    itemParams.accessInformation = "Credits" 
    itemParams.tags = "Maxent"+row['Species'] 
    itemParams.description = "This map depicts the tiled output of a Maxent model depicting the probability of occurrence of "+row['Species']+". An in-line legend is not available for this map. " 
    itemParams.extent = "-84.1076,35.2814,-82.9795, 35.8366" 

    # Add the Web Map 
    # 
    print (user.addItem(itemParameters=itemParams, 
           overwrite=True, 
           text=json.dumps(row))) 
    x = x + 1 

這裏的CSV:

Species,ID 
Abacion_magnum,0000166 
Abaeis_nicippe,0000169 
Abagrotis_alternata,0000172 
Abies_fraseri,0000214 
Ablabesmyia_mallochi,0000223 
Abrostola_ovalis,0000232 
Acalypha_rhomboidea,0000253 
Acanthostigma_filiforme,0000296 
Acanthostigma_minutum,0000297 
Acanthostigma_multiseptatum,0000298 
Acentrella_ampla,0000314 
Acer_negundo,0000330 
Acer_pensylvanicum,0000333 
Acer_rubrum_v_rubrum,0000337 
Acer_rubrum_v_trilobum,0000338 
Acer_saccharum,0000341 
Acer_spicatum,0000343 
+0

調試問題,應該有一個[MCVE。在這種情況下,如果可以刪除與特定錯誤無關的代碼,則可以更容易地找出問題。 –

+1

現在,將x == 0更改爲true。由於它只適用於第一個循環,我的猜測是這個if語句阻止了其餘的行被處理。 –

+1

注意:我建議你用open('TILES.csv','rb')打開你的文件作爲csvfile:' –

回答

1

我覺得你的縮進是錯誤的,你只有你for循環內的if和JSON:

if x == 0: 
     map_json = { 
    "operationalLayers": [ 
    { 
     "templateUrl": "https://{subDomain}.tiles.mapbox.com/v4/abc.GRSM_"+row['ID']+"_pink/{level}/{col}/{row}.png?access_token=pk.secret", 
     "id": "GRSM_SPECIES_OBSERVATIONS_MAXENT_5733", 
     "type": "WebTiledLayer", 
     "layerType": "WebTiledLayer", 
     "title": row['Species']+" Prediction", 
     "copyright": "GRSM", 
     "fullExtent": { 
     "xmin": -20037508.342787, 
     "ymin": -20037508.34278, 
     "xmax": 20037508.34278, 
     "ymax": 20037508.342787, 
     "spatialReference": { 
      "wkid": 102100 
     } 
     }, 
     "subDomains": [ 
     "a", 
     "b", 
     "c", 
     "d" 
     ], 
     "visibility": True, 
     "opacity": 1 
    } 
    ], 
    "baseMap": { 
    "baseMapLayers": [ 
     { 
     "id": "defaultBasemap", 
     "layerType": "ArcGISTiledMapServiceLayer", 
     "opacity": 1, 
     "visibility": True, 
     "url": "http://services.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer" 
     } 
    ], 
    "title": "Topographic" 
    }, 
    "spatialReference": { 
    "wkid": 102100, 
    "latestWkid": 3857 
    }, 
    "version": "2.0" 
} 
+0

最終,這是一個令人頭痛的問題,涉及到JSON格式化縮進縮進,用記事本++解決,並找到/替換空格選項卡。 – tpcolson

0

你之所以可能只有在你的結果中得到一行是因爲你的代碼包含在條件爲x == 0的if語句中。我可以看到你將x設置爲0以外的for循環,並在循環結束時增加x。這導致x不再等於0,因此if語句條件爲false。

嘗試徹底刪除if語句並在最後增加行。 只需使用:

for row in inputFile: 
    # your code here 

這將允許您遍歷CSV文件

+0

感謝您指出......但沒有骰子。成功通過一次迭代激發...移除x = 0#計數器顯示文件計數,如果x == 0:並且x = x + 1 – tpcolson