Select to view content in your preferred language

Replacing text in txt files

978
4
Jump to solution
09-15-2017 11:43 AM
Nicole_Ueberschär
Esri Regular Contributor

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?

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
Nicole_Ueberschär
Esri Regular Contributor

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...

View solution in original post

0 Kudos
4 Replies
DanPatterson_Retired
MVP Emeritus

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. 

Nicole_Ueberschär
Esri Regular Contributor

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...

0 Kudos
DanPatterson_Retired
MVP Emeritus

Yes, it helps to post the full script... it is the only way that people know what you are working with

0 Kudos
Nicole_Ueberschär
Esri Regular Contributor

I'm not sure if it would really help to post the full 250 lines of my script... 

0 Kudos