Select to view content in your preferred language

Replacing Attributes

1708
8
02-12-2013 11:44 AM
BradMartin
Emerging Contributor
I have done this a long time ago, but I forget how I did it. I believe there is a script I can write that says anything left of a certain word or a certain amount of spaces gets deleted and replaced with something else. Can anyone help lead me in the right direction?
Tags (2)
0 Kudos
8 Replies
MathewCoyle
Honored Contributor
I assume you are talking about string methods, split, strip, replace, etc.

http://docs.python.org/2/library/stdtypes.html#string-methods
0 Kudos
BradMartin
Emerging Contributor
That has got me in the right direction, but it is not completely helping me out. For example: 
In the field called "Picture" there are a bunch of directories of where pictures are located. My goal is to get all of the directories to change to the same directory and keep same name for that image.

c:\\Users\Desktop\1\Picture\IMG_001.jpg       >       c:\\Users\Desktop\Pictures\IMG_001.jpg
c:\\Users\Desktop\2\Picture\IMG_002.jpg       >       c:\\Users\Desktop\Pictures\IMG_002.jpg
c:\\Users\Desktop\3\Picture\IMG_003.jpg       >       c:\\Users\Desktop\Pictures\IMG_003.jpg


I have got replace to work, but it is not the complete function I am looking for. I am looking to replace everything 12 spaces from the right with c:\\Users\Desktop\Pictures    I have not found anything to help me with this yet.
0 Kudos
by Anonymous User
Not applicable
That has got me in the right direction, but it is not completely helping me out. For example: 
In the field called "Picture" there are a bunch of directories of where pictures are located. My goal is to get all of the directories to change to the same directory and keep same name for that image.

c:\\Users\Desktop\1\Picture\IMG_001.jpg       >       c:\\Users\Desktop\Pictures\IMG_001.jpg
c:\\Users\Desktop\2\Picture\IMG_002.jpg       >       c:\\Users\Desktop\Pictures\IMG_002.jpg
c:\\Users\Desktop\3\Picture\IMG_003.jpg       >       c:\\Users\Desktop\Pictures\IMG_003.jpg


I have got replace to work, but it is not the complete function I am looking for. I am looking to replace everything 12 spaces from the right with c:\\Users\Desktop\Pictures    I have not found anything to help me with this yet.


This can be done easily using the os.path module.  I made a dummy table with your original paths (Path_1) then added another field (Path_2) to add the new paths.  I then used an update cursor to fix the path names in this table.

import arcpy
from os import path as p   ### less typing, replaces os.path with p

test = r'C:\TestData\PathTest_Table.dbf'

newpath = r'c:\\Users\Desktop\Pictures'
rows = arcpy.UpdateCursor(test)
for row in rows:
    row.Path_2 = p.join(newpath,p.basename(row.Path_1))
    rows.updateRow(row)
del row, rows

print 'done'


And here is a pic of the results
[ATTACH=CONFIG]21920[/ATTACH]
0 Kudos
BradMartin
Emerging Contributor
Caleb,

This is going to be exactly what I want to do. I just have a couple questions about how to execute it. I have tried the process you described within the python window in Arcmap, which did not work. I have also tried It in my python 2.5 command line window, this also did not work. I do not have very much python experience, so a little more guidance would be great.

In the command line window when I tried Import Arcpy it responded with "ImportError: No module named arcpy"

I also get a "Parsing error <type 'exceptions.SyntaxError'>: EOL while scanning string literal (line 1)"
0 Kudos
by Anonymous User
Not applicable
You will have to make a few minor adjustments to get it to work with your data.  When executing scripts in PythonWin inside ArcMap you do not have to import arcpy or use the full path to point to the data.  You can simply use the feature layer or table view name.

However, you mentioned that you used Python 2.5.  I am assuming this means you are running ArcGIS 9.3? if so you will not be able to use the arcpy module because this was not introduced until Arc 10 and it uses Python 2.6.5.

If you do this from a standalone script using the Python 2.5 IDLE, the code in 9.3 should be something like this.  to be safe you may want to add a temp field called Picture2.  You will need to replace the 'table' variable with the full path to your feature class or table.  Make sure to include the "r" before the quotes so it will use raw strings, or else use double backslashes between each folder.  I cannot test this since I do not have 9.3, but I believe this *should* work.

import arcgisscripting
import os
gp = arcgisscripting.create(9.3)

table = r'C:\TestData\Path_to_your_table'

newpath = r'c:\\Users\Desktop\Pictures'
rows = gp.UpdateCursor(table)
for row in rows:
    row.Picture2 = os.path.join(newpath,os.path.basename(row.Picture))
    rows.updateRow(row)
    row = rows.Next()
del row
del rows

print 'done'
0 Kudos
BradMartin
Emerging Contributor
Caleb,

I do have 10

I have tried to use the python window and follow along the initial post that you replied with. I feel like I am very close to getting it to work . I can get something similar to what you got, but I still get an error at the end.

[ATTACH=CONFIG]21941[/ATTACH]
0 Kudos
by Anonymous User
Not applicable
I see several problems here. If you are using the raw string method (puting an "r" in front of the quotes for path names) you do not need to use double backslashes, just use single. Also, it would probably be easier to run this from the scripting window rather than the interactive window. To open the scripting window just go to File > New Window. Then you can copy and paste the full code and execute it by hitting Run Script in the Run tab. You will need to save the script with a .py extension first.

You also did not appear to change the 'test' variable. You need to point this to the table you are trying to update. The r'C:\TestData\PathTest_Table.dbf' was the table I was using as a test. Everything you will need to change is highlighted in red below:

import arcpy
from os import path as p   ### less typing, replaces os.path with p

test = r'C:\TestData\PathTest_Table.dbf'  # change this to point to your table

newpath = r'c:\\Users\Desktop\Pictures'
rows = arcpy.UpdateCursor(test)
for row in rows:
    row.Test_1 = p.join(newpath,p.basename(row.Context_ph))  # Make sure you have the correct field names after "row."
    rows.updateRow(row)
del row, rows

print 'done'


If you have Arc 10 you should just be able to paste this code in the scripting window and make the suggested changes to get it to work.

Also, do you need to double backslashes in this? c:\\Users\Desktop\Pictures
0 Kudos
BradMartin
Emerging Contributor
Thank you so much!!!
0 Kudos