<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Blog.hio.fr &#187; strReplace</title>
	<atom:link href="http://blog.hio.fr/tag/strreplace/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.hio.fr</link>
	<description></description>
	<lastBuildDate>Tue, 07 Sep 2010 04:42:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=abc</generator>
		<item>
		<title>PYTHON str_replace en python</title>
		<link>http://blog.hio.fr/2009/11/15/python-str_replace-en-python/</link>
		<comments>http://blog.hio.fr/2009/11/15/python-str_replace-en-python/#comments</comments>
		<pubDate>Sun, 15 Nov 2009 21:36:28 +0000</pubDate>
		<dc:creator>HiO</dc:creator>
				<category><![CDATA[Dev]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[strReplace]]></category>
		<category><![CDATA[str_replace]]></category>

		<guid isPermaLink="false">http://blog.hio.fr/?p=480</guid>
		<description><![CDATA[Comment faire un equivalent de str_replace() de php en python]]></description>
			<content:encoded><![CDATA[<p>Comment faire un equivalent de str_replace() de php en python</p>
<p><strong>La fonction strReplace</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">os</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">string</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">shutil</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> strReplace<span style="color: black;">&#40;</span><span style="color: #008000;">str</span>, <span style="color: #dc143c;">array</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">for</span> search, replace <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #dc143c;">array</span>:
        <span style="color: #008000;">str</span> = <span style="color: #008000;">str</span>.<span style="color: black;">replace</span><span style="color: black;">&#40;</span>search, replace<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">str</span></pre></td></tr></table></div>

<p><strong>Utilisation</strong></p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">replace = <span style="color: black;">&#91;</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">' '</span>, <span style="color: #483d8b;">'_'</span><span style="color: black;">&#41;</span>, <span style="color: black;">&#40;</span><span style="color: #483d8b;">'-'</span>, <span style="color: #483d8b;">''</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">for</span> <span style="color: #008000;">dir</span> <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">listdir</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'.'</span><span style="color: black;">&#41;</span>:
    newdir = strReplace<span style="color: black;">&#40;</span><span style="color: #008000;">dir</span>, replace<span style="color: black;">&#41;</span>.<span style="color: black;">capitalize</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;moving: %s &gt; %s&quot;</span><span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span><span style="color: #008000;">dir</span>, newdir<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    <span style="color: #dc143c;">os</span>.<span style="color: black;">rename</span><span style="color: black;">&#40;</span><span style="color: #008000;">dir</span>, newdir<span style="color: black;">&#41;</span></pre></div></div>

<p><strong>ce qui nous donne dans ce cas précis</strong></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">moving: Angel of Retribution <span style="color: #000000; font-weight: bold;">&gt;</span> Angel_of_retribution
moving: Living After Midnight <span style="color: #000000; font-weight: bold;">&gt;</span> Living_after_midnight
moving: British Steel <span style="color: #000000; font-weight: bold;">&gt;</span> British_steel
moving: Defenders Of The Faith <span style="color: #000000; font-weight: bold;">&gt;</span> Defenders_of_the_faith
moving: Killing Machine <span style="color: #000000; font-weight: bold;">&gt;</span> Killing_machine
moving: Ram It Down <span style="color: #000000; font-weight: bold;">&gt;</span> Ram_it_down
moving: Hell Bent For Leather <span style="color: #000000; font-weight: bold;">&gt;</span> Hell_bent_for_leather
moving: Stained Class <span style="color: #000000; font-weight: bold;">&gt;</span> Stained_class
moving: Sin After Sin <span style="color: #000000; font-weight: bold;">&gt;</span> Sin_after_sin
moving: Jugulator <span style="color: #000000; font-weight: bold;">&gt;</span> Jugulator
moving: Screaming For Vengeance <span style="color: #000000; font-weight: bold;">&gt;</span> Screaming_for_vengeance
moving: Turbo <span style="color: #000000; font-weight: bold;">&gt;</span> Turbo
moving: Painkiller <span style="color: #000000; font-weight: bold;">&gt;</span> Painkiller
moving: Rocka Rolla <span style="color: #000000; font-weight: bold;">&gt;</span> Rocka_rolla
moving: Point Of Entry <span style="color: #000000; font-weight: bold;">&gt;</span> Point_of_entry
moving: Demolition <span style="color: #000000; font-weight: bold;">&gt;</span> Demolition
moving: Priest...Live<span style="color: #000000; font-weight: bold;">!</span> <span style="color: #000000; font-weight: bold;">&gt;</span> Priest...live<span style="color: #000000; font-weight: bold;">!</span>
moving: Sad Wings Of Destiny <span style="color: #000000; font-weight: bold;">&gt;</span> Sad_wings_of_destiny</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.hio.fr/2009/11/15/python-str_replace-en-python/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
