<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Replace words\characters in text file in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/replace-words-characters-in-text-file/m-p/1189334#M64904</link>
    <description>&lt;P&gt;Hi, I'm trying to replace some characters within a lyrx file.&lt;/P&gt;&lt;P&gt;In this example I need to replace "570" with "560"&lt;/P&gt;&lt;P&gt;The problem I have is that two lines are identical but I only need to replace one (highlighted in yellow in the image below) and not the one a few lines above the highlighted one.&lt;/P&gt;&lt;P&gt;The code I'm using:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;for i, line in enumerate(fileinput.input(OUT_workspace + "\\" + "AE000S560_005M5001P01M041.lyrx", inplace=1)):
    sys.stdout.write(line.replace('"max" : 570', '"max" : 560'))&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would really appreciate any help. I'm not sure how to target a specific line within the file. Many thanks.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture.PNG" style="width: 475px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/45181iCD0E2F68A3274F2A/image-dimensions/475x353?v=v2" width="475" height="353" role="button" title="Capture.PNG" alt="Capture.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
    <pubDate>Tue, 05 Jul 2022 11:01:58 GMT</pubDate>
    <dc:creator>anTonialcaraz</dc:creator>
    <dc:date>2022-07-05T11:01:58Z</dc:date>
    <item>
      <title>Replace words\characters in text file</title>
      <link>https://community.esri.com/t5/python-questions/replace-words-characters-in-text-file/m-p/1189334#M64904</link>
      <description>&lt;P&gt;Hi, I'm trying to replace some characters within a lyrx file.&lt;/P&gt;&lt;P&gt;In this example I need to replace "570" with "560"&lt;/P&gt;&lt;P&gt;The problem I have is that two lines are identical but I only need to replace one (highlighted in yellow in the image below) and not the one a few lines above the highlighted one.&lt;/P&gt;&lt;P&gt;The code I'm using:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;for i, line in enumerate(fileinput.input(OUT_workspace + "\\" + "AE000S560_005M5001P01M041.lyrx", inplace=1)):
    sys.stdout.write(line.replace('"max" : 570', '"max" : 560'))&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would really appreciate any help. I'm not sure how to target a specific line within the file. Many thanks.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture.PNG" style="width: 475px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/45181iCD0E2F68A3274F2A/image-dimensions/475x353?v=v2" width="475" height="353" role="button" title="Capture.PNG" alt="Capture.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 05 Jul 2022 11:01:58 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/replace-words-characters-in-text-file/m-p/1189334#M64904</guid>
      <dc:creator>anTonialcaraz</dc:creator>
      <dc:date>2022-07-05T11:01:58Z</dc:date>
    </item>
    <item>
      <title>Re: Replace words\characters in text file</title>
      <link>https://community.esri.com/t5/python-questions/replace-words-characters-in-text-file/m-p/1189342#M64905</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/120914"&gt;@anTonialcaraz&lt;/a&gt;,&lt;/P&gt;&lt;P&gt;I assume that you are using python. If that is the case, another option instead of using replace would be to create a dictionary so that the set values in the dictionary are overwritten to the value that you specify.&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;maxvalue = {"max": 560}
for i, line in enumerate(fileinput.input(OUT_workspace + "\\" + "AE000S560_005M5001P01M041.lyrx", inplace=1)):
    #insert line exception below for specific line to update
    if i ==(line number to change):
        maxvalue["max"] = 570
        sys.stdout.write(maxvalue["max"])&lt;/LI-CODE&gt;&lt;P&gt;Without knowing the code specifics, this is the only suggestion I can come up with at the moment.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 05 Jul 2022 12:04:50 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/replace-words-characters-in-text-file/m-p/1189342#M64905</guid>
      <dc:creator>RPGIS</dc:creator>
      <dc:date>2022-07-05T12:04:50Z</dc:date>
    </item>
    <item>
      <title>Re: Replace words\characters in text file</title>
      <link>https://community.esri.com/t5/python-questions/replace-words-characters-in-text-file/m-p/1189346#M64906</link>
      <description>&lt;P&gt;Thanks very much&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/22623"&gt;@RPGIS&lt;/a&gt;, I appreciate.&lt;/P&gt;&lt;P&gt;Yes, I'm using Python. I've tried this:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;maxvalue = {"max": 570}
for i, line in enumerate(fileinput.input(OUT_workspace + "\\" + "AE000S560_005M5001P01M041.lyrx", inplace=1)):
    #insert line exception below for specific line to update
    if i ==(706):
        maxvalue["max"] = "560"
        sys.stdout.write(maxvalue["max"])&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;But the result I'm getting is simply "560". All the rest of the contents in the .lyrx file are gone.&lt;/P&gt;&lt;P&gt;Any suggestions?&lt;/P&gt;</description>
      <pubDate>Tue, 05 Jul 2022 12:24:43 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/replace-words-characters-in-text-file/m-p/1189346#M64906</guid>
      <dc:creator>anTonialcaraz</dc:creator>
      <dc:date>2022-07-05T12:24:43Z</dc:date>
    </item>
    <item>
      <title>Re: Replace words\characters in text file</title>
      <link>https://community.esri.com/t5/python-questions/replace-words-characters-in-text-file/m-p/1189374#M64908</link>
      <description>&lt;P&gt;You would need to access the &lt;STRONG&gt;&lt;EM&gt;activeSlice&lt;/EM&gt; &lt;/STRONG&gt;dictionary(wherever that is being stored), then access the &lt;EM&gt;&lt;STRONG&gt;rangeDimensionValue&lt;/STRONG&gt;&amp;nbsp;dictionary to&amp;nbsp;&lt;/EM&gt;set the &lt;EM&gt;&lt;STRONG&gt;max&lt;/STRONG&gt; &lt;/EM&gt;within that dictionary. To do that, you would simply need to find where the&amp;nbsp;&lt;STRONG&gt;&lt;EM&gt;activeSlice&amp;nbsp;&lt;/EM&gt;&lt;/STRONG&gt;is the key and then access the values within that key. Then access the&amp;nbsp;&lt;EM&gt;&lt;STRONG&gt;rangeDimensionValue &lt;/STRONG&gt;&lt;/EM&gt;values&amp;nbsp;to then set the&amp;nbsp;&lt;EM&gt;&lt;STRONG&gt;max&lt;/STRONG&gt;&lt;/EM&gt;&lt;EM&gt;&amp;nbsp;&lt;/EM&gt;to the specified value.&lt;/P&gt;&lt;P&gt;You may be able to access that particular value as such:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;"activeSlice"["rangeDimensionValue"]["max"]= maxvalue["max"]&lt;/LI-CODE&gt;&lt;P&gt;I can't tell (based on the png) if those values are in both lists and dictionaries, or a combination of sorts. If you can find the&amp;nbsp;&lt;STRONG&gt;&lt;EM&gt;activeSlice&amp;nbsp;&lt;/EM&gt;&lt;/STRONG&gt;key, then you should be able to use the suggestion above to change the max value.&lt;/P&gt;</description>
      <pubDate>Tue, 05 Jul 2022 13:24:09 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/replace-words-characters-in-text-file/m-p/1189374#M64908</guid>
      <dc:creator>RPGIS</dc:creator>
      <dc:date>2022-07-05T13:24:09Z</dc:date>
    </item>
    <item>
      <title>Re: Replace words\characters in text file</title>
      <link>https://community.esri.com/t5/python-questions/replace-words-characters-in-text-file/m-p/1189399#M64909</link>
      <description>&lt;P&gt;&lt;SPAN&gt;Hi, why don’t you parse the lyrx and then you can address the “rangeDimensionValue” directly?&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 05 Jul 2022 13:54:26 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/replace-words-characters-in-text-file/m-p/1189399#M64909</guid>
      <dc:creator>TomGeo</dc:creator>
      <dc:date>2022-07-05T13:54:26Z</dc:date>
    </item>
    <item>
      <title>Re: Replace words\characters in text file</title>
      <link>https://community.esri.com/t5/python-questions/replace-words-characters-in-text-file/m-p/1189410#M64912</link>
      <description>&lt;P&gt;Couple things here- fileinput will basically create an list of lines from the text file so I don't think you can access the 'keys' as structured in the png or python dictionary. Enumerate creates an inplace tuple containing the index number and value (line).&lt;/P&gt;&lt;P&gt;Meaning line 706 will just look like '"max": 570' when you are iterating over it. To replace it, use '"max": 560'. Also, if you don't put anything back to the file if it doesn't match in the if, fileinput erases it which is probably why you only ended up with 560.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;for i, line in enumerate(fileinput.input(OUT_workspace + "\\" + "AE000S560_005M5001P01M041.lyrx", inplace=1)):
    #insert line exception below for specific line to update
    if i == 760:
        sys.stdout.write("max": 560) # or '"max": 560', im not sure which would work
    else:
        sys.stdout.write(line)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 05 Jul 2022 14:07:38 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/replace-words-characters-in-text-file/m-p/1189410#M64912</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2022-07-05T14:07:38Z</dc:date>
    </item>
    <item>
      <title>Re: Replace words\characters in text file</title>
      <link>https://community.esri.com/t5/python-questions/replace-words-characters-in-text-file/m-p/1189437#M64919</link>
      <description>&lt;P&gt;Thanks again&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/22623"&gt;@RPGIS&lt;/a&gt;.&lt;/P&gt;&lt;P&gt;I'm not very familiar with the activeSlice dictionary concept unfortunately. Must admit I'm a bit lost now.&lt;/P&gt;&lt;P&gt;Please let me pass you the .lyrx file see if that makes things easier. I just need to replace the rangeDimensionValue of several files. For example, from 570 to 560. I was initially using line.replace but that would change the "customFullExtent" part as well and I need that to keep the same values.&lt;/P&gt;</description>
      <pubDate>Tue, 05 Jul 2022 14:36:45 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/replace-words-characters-in-text-file/m-p/1189437#M64919</guid>
      <dc:creator>anTonialcaraz</dc:creator>
      <dc:date>2022-07-05T14:36:45Z</dc:date>
    </item>
    <item>
      <title>Re: Replace words\characters in text file</title>
      <link>https://community.esri.com/t5/python-questions/replace-words-characters-in-text-file/m-p/1189456#M64921</link>
      <description>&lt;P&gt;The&amp;nbsp;&lt;SPAN&gt;activeSlice&amp;nbsp;is a specific key storing all other values that are assigned to that key.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;A href="https://www.w3schools.com/python/python_dictionaries.asp" target="_blank" rel="noopener"&gt;Python Dictionaries (w3schools.com)&lt;/A&gt;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 05 Jul 2022 15:23:39 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/replace-words-characters-in-text-file/m-p/1189456#M64921</guid>
      <dc:creator>RPGIS</dc:creator>
      <dc:date>2022-07-05T15:23:39Z</dc:date>
    </item>
    <item>
      <title>Re: Replace words\characters in text file</title>
      <link>https://community.esri.com/t5/python-questions/replace-words-characters-in-text-file/m-p/1189499#M64923</link>
      <description>&lt;P&gt;Treat the entire file as one string with newlines embedded in it, then use "replace".&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;lyrx is the string containing the entire file&lt;/P&gt;&lt;P&gt;Line 3 looks for the pattern of 560 on a line and "max" on the next line then replaces that with the new value&lt;/P&gt;&lt;LI-CODE lang="python"&gt;with open("file.lyrx") as fp: 
  lyrx = fp.read()
lyrx.replace('560,\n"max"', '570,\n"max"'))&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If you want a more general approach that deals with random whitespace you could use the re.sub instead, you can go down a real rabbit hole with regex though so use plain old replace unless you are getting fancy.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import re
fixed = re.sub('560,\s*"max"','570,\n"max"',lyrx)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 05 Jul 2022 16:50:23 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/replace-words-characters-in-text-file/m-p/1189499#M64923</guid>
      <dc:creator>Brian_Wilson</dc:creator>
      <dc:date>2022-07-05T16:50:23Z</dc:date>
    </item>
    <item>
      <title>Re: Replace words\characters in text file</title>
      <link>https://community.esri.com/t5/python-questions/replace-words-characters-in-text-file/m-p/1189748#M64931</link>
      <description>&lt;P&gt;Thanks Brian. Does not seem to work unfortunately as the values don't change. Please see the code.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy
import os
import string
import sys
import fileinput
from arcpy import env
arcpy.env.overwriteOutput=True
from arcpy.sa import*





env.workspace = r"H:\PROGRAMMES\10_OTHER_PROJECTS\26_04072022_KEV"
OUT_workspace = r"H:\PROGRAMMES\10_OTHER_PROJECTS\26_04072022_KEV_NEW"

Lyrx = "AE000S570_005M5001P01M041.lyrx"

# Copy new file into new folder

arcpy.Copy_management(Lyrx, OUT_workspace + "\\" + "AE000S570_005M5001P01M041.lyrx")
arcpy.Copy_management(Lyrx, OUT_workspace + "\\" + "AE000S560_005M5001P01M041.lyrx")


with open(OUT_workspace + "\\" + "AE000S560_005M5001P01M041.lyrx") as fp:
    lyrx = fp.read()
lyrx.replace('570,\n"max"', '560,\n"max"')&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 06 Jul 2022 10:43:34 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/replace-words-characters-in-text-file/m-p/1189748#M64931</guid>
      <dc:creator>anTonialcaraz</dc:creator>
      <dc:date>2022-07-06T10:43:34Z</dc:date>
    </item>
    <item>
      <title>Re: Replace words\characters in text file</title>
      <link>https://community.esri.com/t5/python-questions/replace-words-characters-in-text-file/m-p/1191267#M64980</link>
      <description>&lt;P&gt;You need to write the changed string back out to a file, you could do this, and avoid loading all those libraries you don't need too. Loading "arcpy" takes forever.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import os

IN_workspace = r"H:\PROGRAMMES\10_OTHER_PROJECTS\26_04072022_KEV"
OUT_workspace = r"H:\PROGRAMMES\10_OTHER_PROJECTS\26_04072022_KEV_NEW"

Lyrx = "AE000S570_005M5001P01M041.lyrx"

with open(os.path.join(IN_workspace, Lyrx), "r") as fp:
    lyrx = fp.read()

fixed = lyrx.replace('570,\n"max"', '560,\n"max"')
if lyrx == fixed:
    print("WARNING, NOTHING CHANGED!")

with open(os.path.join(OUT_workspace, Lyrx), "w") as fp:
    fp.write(fixed)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I used "os.path.join" to build up the file paths just because it is tidier, sometimes that avoids problems with missed slashes and stuff like that. It's OS independent too.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I avoid relying on the arcgis "workspace" because about 1/2 the Esri tools just ignore it.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 11 Jul 2022 15:34:59 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/replace-words-characters-in-text-file/m-p/1191267#M64980</guid>
      <dc:creator>Brian_Wilson</dc:creator>
      <dc:date>2022-07-11T15:34:59Z</dc:date>
    </item>
  </channel>
</rss>

