<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"> ...<br>
<customColorRamp><br>
<colorRampType>LINEAR</colorRampType><br>
<colorRampEntry red="0"
blue="143" value="1.000000" green="0"
label="Color entry 1" /><br>
<colorRampEntry red="0"
blue="159" value="2.000000" green="0"
label="Color entry 2" /><br>
<colorRampEntry red="0"
blue="175" value="3.000000" green="0"
label="Color entry 3" /><br>
...<br><br>
</font>This is what i exec in Python Console:<br>
<font face="Courier New, Courier">>>> fIn =
open("C:\\Users\\Michele\\.qgis\\python\\plugins\\KitICT\\style\\colormap64.qml","r")<br>
...<br>
>>> szLine = fIn.readline()<br>
>>> delta = (54.0-42.0+1)/64.0<br>
>>> i=0<br>
>>> szToSearch = "value=\"" + str(i+1) +
".000000\""<br>
>>> szToReplace = "value=\"" + str(round(42.0 +
i*delta,6)) + "\""<br>
>>> szLine.replace(szToSearch, szToReplace)<br>
<colorRampEntry red="0" blue="143"
value="42.0" green="0" label="Color entry
1" /> <br><br>
</font>And this is my function:<br>
<font face="Courier New, Courier"> ...<br>
fIn = open(szFileIn,
</font><font face="Courier New, Courier" color="#00AA00"><i>
"r"</i></font><font face="Courier New, Courier">)<br>
fOut = open(szFileOut,
</font><font face="Courier New, Courier" color="#00AA00"><i>
"w"</i></font><font face="Courier New, Courier">)<br><br>
i =
</font><font face="Courier New, Courier" color="#800000">0<br>
</font><font face="Courier New, Courier"> 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"> szLine =
fIn.readline()<br>
</font><font face="Courier New, Courier" color="#0000FF">while</font>
<font face="Courier New, Courier"> len(szLine) >
</font><font face="Courier New, Courier" color="#800000">0</font>
<font face="Courier New, Courier">:<br>
</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>
"<colorRampEntry"</i></font>
<font face="Courier New, Courier">) >
-</font><font face="Courier New, Courier" color="#800000">1</font>
<font face="Courier New, Courier">:<br>
szToSearch =
</font><font face="Courier New, Courier" color="#00AA00"><i>
"value=\""</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>
".000000\""<br>
</i></font><font face="Courier New, Courier">
szToReplace =
</font><font face="Courier New, Courier" color="#00AA00"><i>
"value=\""</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>
"\""<br>
</i></font><font face="Courier New, Courier">
szLine
= str(szLine)<br><br>
szLine.replace(szToSearch, szToReplace)<br>
fOut.write(szLine)<br>
i =
i+</font><font face="Courier New, Courier" color="#800000">1<br>
</font><font face="Courier New, Courier">
</font><font face="Courier New, Courier" color="#0000FF">else</font>
<font face="Courier New, Courier">:<br>
fOut.write(szLine)<br>
</font><font face="Courier New, Courier" color="#0000FF">pass<br>
</font><font face="Courier New, Courier">
szLine = fIn.readline()<br>
</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>