Python:从csv读取时for循环在第一行停止

时间:2016-02-12 16:57:18

标签: python for-loop gis mapbox arcgis

使用此代码,我可以通过csv中的第一行触发它并发布内容。没有for循环,它也很好用。我也使用简单的print语句打印出csv中的所有行。我遇到的问题是如何通过我的csv(2300行)循环并替换两个内联变量。我已经尝试过几次迭代,移动语句等,这是我最近的尝试。

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

2 个答案:

答案 0 :(得分: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"
}

答案 1 :(得分:0)

你可能只在结果中获得一行的原因是因为你有一个if语句中的代码,条件是x == 0.我可以看到你在for循环之外将x设置为0在循环结束时,您正在递增x。这导致x不再等于0,因此if语句条件为false。

尝试完全删除if语句,并在结尾处删除增量行。 只需使用:

for row in inputFile:
    # your code here

这将允许您循环遍历csv文件