I am replacing square brackets by round brackets with help of a python script - works perfectly.
But I also want to replace a single word by another word (Cruise -> Campaign): nothing happens.
Here is the part of the script:
r = csv.reader(t, delimiter="\t")
all=[]
header=r.next()
#arcpy.AddMessage(header)
for fields in header:
fields.replace("Cruise","Campaign")##not working
fields.replace("[", "(")
fields.replace("]", ")")
Any idea why it replaces the brackets fine but not the word?
Solved! Go to Solution.
Thanks Dan,
I have to work with txt files.
Both, the brackets and the word "Cruise" are only to find in the header of the txt file(s).
I think I was trying it in the wrong place. Obviously I had the lines below already in a different position in my script (except of the Cruise line) which worked and the one I posted above didn't do anything. So when I added now the Cruise replacement it worked.
with open(tablefile, 'r') as txtfile:
txtdata=txtfile.read()
txtdata=txtdata.replace("[","(")
txtdata=txtdata.replace("]",")")
txtdata=txtdata.replace("Cruise","Campaign")
with open(tablefile, 'w') as txtfile:
txtfile.write(txtdata)
I knew it must have been my mistake...
post the line in question... the details will be found there.
case-sensitivity needs to be considered as well
Addendum
As a followup, I had assumed you were trying to replace the field name with a new value, I suspect that you want to replace the values in the csv file with the new value.
I would suggest that converting it to a featureclass, then use the calculate field tool (accessed through your script) to perform the conversion.
Thanks Dan,
I have to work with txt files.
Both, the brackets and the word "Cruise" are only to find in the header of the txt file(s).
I think I was trying it in the wrong place. Obviously I had the lines below already in a different position in my script (except of the Cruise line) which worked and the one I posted above didn't do anything. So when I added now the Cruise replacement it worked.
with open(tablefile, 'r') as txtfile:
txtdata=txtfile.read()
txtdata=txtdata.replace("[","(")
txtdata=txtdata.replace("]",")")
txtdata=txtdata.replace("Cruise","Campaign")
with open(tablefile, 'w') as txtfile:
txtfile.write(txtdata)
I knew it must have been my mistake...
Yes, it helps to post the full script... it is the only way that people know what you are working with
I'm not sure if it would really help to post the full 250 lines of my script...