Store URLs from a field into a list using arcpy

102
1
Jump to solution
2 weeks ago
Ed_
by MVP Regular Contributor
MVP Regular Contributor
  • I am trying to read a geodatabase table into a script.
  • Store URLs from Item_URL field into a list

Stuck ok step 2, how can I fix this?

import arcpy
import pandas as pd
from datetime import datetime, timezone
import pandas
import urllib
import http
import sys

# Import the geodatabase table
ItemsURL = "O:\\CheckURLs\\MyProject.gdb\\HubitemCatalogList"

# Select the fields to print
fields =  ['Item_URL']

with arcpy.da.SearchCursor(ItemsURL, fields) as cursor:
    for i in range(5):
        for row in cursor:
            print(row)
            break
    print('Geodatabase table was read in just fine :)')    

# Create a list of URLs from Item_URL field
URL_List = []
URL_List = ItemsURL['Item_URL']
Print('List created successfully')

 

Error:

 URL_List = ItemsURL['Item_URL']
TypeError: string indices must be integers

 

Question | Analyze | Visualize
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
AlfredBaldenweck
MVP Regular Contributor

Right now, you're asking it to return an index from the string 

O:\\CheckURLs\\MyProject.gdb\\HubitemCatalogList"

So like, ItemsURL[0] would return"O". This is not what you're trying to do.

Try this instead?

 Basically, bring the URL_List up and then append each value into it as you iterate over the rows.

import arcpy
import pandas as pd
from datetime import datetime, timezone
import pandas
import urllib
import http
import sys

# Import the geodatabase table
ItemsURL = "O:\\CheckURLs\\MyProject.gdb\\HubitemCatalogList"

# Select the fields to print
fields =  ['Item_URL']
URL_List = []
with arcpy.da.SearchCursor(ItemsURL, fields) as cursor:
    for i in range(5):
        for row in cursor:
            print(row)
            # Add the Item_URL value to the list.
            URL_List.append(row[0])
            break
    print('Geodatabase table was read in just fine :)')    
print('List created successfully')
print(URL_List)

 

View solution in original post

1 Reply
AlfredBaldenweck
MVP Regular Contributor

Right now, you're asking it to return an index from the string 

O:\\CheckURLs\\MyProject.gdb\\HubitemCatalogList"

So like, ItemsURL[0] would return"O". This is not what you're trying to do.

Try this instead?

 Basically, bring the URL_List up and then append each value into it as you iterate over the rows.

import arcpy
import pandas as pd
from datetime import datetime, timezone
import pandas
import urllib
import http
import sys

# Import the geodatabase table
ItemsURL = "O:\\CheckURLs\\MyProject.gdb\\HubitemCatalogList"

# Select the fields to print
fields =  ['Item_URL']
URL_List = []
with arcpy.da.SearchCursor(ItemsURL, fields) as cursor:
    for i in range(5):
        for row in cursor:
            print(row)
            # Add the Item_URL value to the list.
            URL_List.append(row[0])
            break
    print('Geodatabase table was read in just fine :)')    
print('List created successfully')
print(URL_List)