<html>
<body>
It's easy to assign the result of the replece to the same
variable....<br>
<font face="Courier New, Courier">szLine = szLine.replace(szToSearch,
szToReplace)<br><br>
</font>Hi all,<br><br>
I have another problem related to the string replacement in QGis.<br>
I want to read a QGis Style file at runtime and write in another file
some modifications.<br><br>
If I try to execute the single command in Python Console everything is
working and my line is changed, while if I execute it in my plugin
nothing happen.<br><br>
Do you know where i made a mistake?<br><br>
This is a part of the file:<br>
<font face="Courier New, Courier">&nbsp;&nbsp;&nbsp; ...<br>
&nbsp;&nbsp;&nbsp; &lt;customColorRamp&gt;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&lt;colorRampType&gt;LINEAR&lt;/colorRampType&gt;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;colorRampEntry red=&quot;0&quot;
blue=&quot;143&quot; value=&quot;1.000000&quot; green=&quot;0&quot;
label=&quot;Color entry 1&quot; /&gt;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;colorRampEntry red=&quot;0&quot;
blue=&quot;159&quot; value=&quot;2.000000&quot; green=&quot;0&quot;
label=&quot;Color entry 2&quot; /&gt;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;colorRampEntry red=&quot;0&quot;
blue=&quot;175&quot; value=&quot;3.000000&quot; green=&quot;0&quot;
label=&quot;Color entry 3&quot; /&gt;<br>
&nbsp;&nbsp;&nbsp; ...<br><br>
</font>This is what i exec in Python Console:<br>
<font face="Courier New, Courier">&gt;&gt;&gt; fIn =
open(&quot;C:\\Users\\Michele\\.qgis\\python\\plugins\\KitICT\\style\\colormap64.qml&quot;,&quot;r&quot;)<br>
...<br>
&gt;&gt;&gt; szLine = fIn.readline()<br>
&gt;&gt;&gt; delta = (54.0-42.0+1)/64.0<br>
&gt;&gt;&gt; i=0<br>
&gt;&gt;&gt; szToSearch = &quot;value=\&quot;&quot; + str(i+1) +
&quot;.000000\&quot;&quot;<br>
&gt;&gt;&gt; szToReplace = &quot;value=\&quot;&quot; + str(round(42.0 +
i*delta,6)) + &quot;\&quot;&quot;<br>
&gt;&gt;&gt; szLine.replace(szToSearch, szToReplace)<br>
&lt;colorRampEntry red=&quot;0&quot; blue=&quot;143&quot;
value=&quot;42.0&quot; green=&quot;0&quot; label=&quot;Color entry
1&quot; /&gt; <br><br>
</font>And this is my function:<br>
<font face="Courier New, Courier">&nbsp;&nbsp;&nbsp; ...<br>
&nbsp;&nbsp;&nbsp; fIn&nbsp; = open(szFileIn,
</font><font face="Courier New, Courier" color="#00AA00"><i>
&quot;r&quot;</i></font><font face="Courier New, Courier">)<br>
&nbsp;&nbsp;&nbsp; fOut = open(szFileOut,
</font><font face="Courier New, Courier" color="#00AA00"><i>
&quot;w&quot;</i></font><font face="Courier New, Courier">)<br><br>
&nbsp;&nbsp;&nbsp; i =
</font><font face="Courier New, Courier" color="#800000">0<br>
</font><font face="Courier New, Courier">&nbsp;&nbsp;&nbsp; delta =
(nMax-nMin+</font><font face="Courier New, Courier" color="#800000">
1</font><font face="Courier New, Courier">)/</font>
<font face="Courier New, Courier" color="#800000">64<br><br>
</font><font face="Courier New, Courier">&nbsp;&nbsp;&nbsp; szLine =
fIn.readline()<br>
&nbsp;&nbsp;&nbsp;
</font><font face="Courier New, Courier" color="#0000FF">while</font>
<font face="Courier New, Courier"> len(szLine) &gt;
</font><font face="Courier New, Courier" color="#800000">0</font>
<font face="Courier New, Courier">:<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</font><font face="Courier New, Courier" color="#0000FF">if</font>
<font face="Courier New, Courier">
szLine.find(</font><font face="Courier New, Courier" color="#00AA00"><i>
&quot;&lt;colorRampEntry&quot;</i></font>
<font face="Courier New, Courier">) &gt;
-</font><font face="Courier New, Courier" color="#800000">1</font>
<font face="Courier New, Courier">:<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
szToSearch =
</font><font face="Courier New, Courier" color="#00AA00"><i>
&quot;value=\&quot;&quot;</i></font><font face="Courier New, Courier"> +
str(i+</font><font face="Courier New, Courier" color="#800000">1</font>
<font face="Courier New, Courier">) +
</font><font face="Courier New, Courier" color="#00AA00"><i>
&quot;.000000\&quot;&quot;<br>
</i></font><font face="Courier New, Courier">
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
szToReplace =
</font><font face="Courier New, Courier" color="#00AA00"><i>
&quot;value=\&quot;&quot;</i></font><font face="Courier New, Courier"> +
str(round(nMin +
i*delta,</font><font face="Courier New, Courier" color="#800000">6</font>
<font face="Courier New, Courier">)) +
</font><font face="Courier New, Courier" color="#00AA00"><i>
&quot;\&quot;&quot;<br>
</i></font><font face="Courier New, Courier">
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; szLine
= str(szLine)<br><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
szLine.replace(szToSearch, szToReplace)<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
fOut.write(szLine)<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; i =
i+</font><font face="Courier New, Courier" color="#800000">1<br>
</font><font face="Courier New, Courier">
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</font><font face="Courier New, Courier" color="#0000FF">else</font>
<font face="Courier New, Courier">:<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
fOut.write(szLine)<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</font><font face="Courier New, Courier" color="#0000FF">pass<br>
</font><font face="Courier New, Courier">
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; szLine = fIn.readline()<br>
&nbsp;&nbsp;&nbsp;
</font><font face="Courier New, Courier" color="#0000FF">pass<br><br>
</font>Where szFileIn and szFileOut are the path if the file to
read/write and nMax/nMin are the minumun and maximum coming from
bandStatistics.<br><br>
I'm using QGis 1.0.2 Kore and I'm developing with Python.<br><br>
<br>
</body>
</html>