Select to view content in your preferred language

How to Read variable as path name

689
1
Jump to solution
11-09-2012 09:01 AM
MaryM
by
Deactivated User
I want make a dictionary from a text file of: layer name,layer file location and then loop through is making variables called the layer name and assigning them arcpy.mapping.Layer(r"layer file location"). I am new to Python, how do I put in the variable name where the layer file location is supposed to be correctly and can I do that?

Here is my code I have so far...

#Pull in a dictionary from reading text file.
#Note:Have a text file ready named file.txt that contains lines of: layer name,layer file location.
#Assign the text file a variable name
TemplateLayers=r("'\\C:\etc\file.text")
#Make the dictionary.
lyrList={}
#Assign the values in the text file to the dictionary.
for line in TemplateLayers:
k,v=line.strip().split(',')
lyrList[k,strip()]=v.strip()
f.close()
#Make the variables from the layer name and assign them a file layer.
for k, v in lyrList.items():
k=arcpy.mapping.Layer(r ----where I am stuck!)
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
MaryM
by
Deactivated User
Nevermind, I did not need to do all that sits above.  I changed to this...

TemplateLayers=(r"\\C\etc\file.txt")
lyrDict={}
with open (TemplateLayers) as f:
    for line in f:
        (key,val)=line.split(",")
        lyrList[(key)]=val
f.close()

print lyrDict

View solution in original post

0 Kudos
1 Reply
MaryM
by
Deactivated User
Nevermind, I did not need to do all that sits above.  I changed to this...

TemplateLayers=(r"\\C\etc\file.txt")
lyrDict={}
with open (TemplateLayers) as f:
    for line in f:
        (key,val)=line.split(",")
        lyrList[(key)]=val
f.close()

print lyrDict
0 Kudos