08-13-2020, 04:49 PM
Python One Line Replace
<div><p><strong>Problem</strong>: You use Python in a terminal and you want to replace a string <code>'example'</code> in a text file <code>file.txt</code>:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">xxxxx example xxxxx</pre>
<p>Your goal is to accomplish the following text:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">xxxxx replaced_example xxxxx</pre>
<p>In particular, you want to open the file, replace the text, and rewrite the result into the file—all in a single line of Python code!</p>
<p><strong><em>Can a <a href="https://blog.finxter.com/python-one-line-x/" target="_blank" rel="noreferrer noopener" title="Python One Line X">Python one-liner</a> accomplish this?</em></strong></p>
<p><strong>Answer</strong>: Yes! You can <a href="https://blog.finxter.com/how-to-execute-multiple-lines-in-a-single-line-python-from-command-line/" title="How to Execute Multiple Lines in a Single Line Python From Command-Line?" target="_blank" rel="noreferrer noopener">compress any Python script</a> into a single line. For computer science nerds: <em>the single line of Python code is <a href="https://simple.wikipedia.org/wiki/Turing_complete" target="_blank" rel="noreferrer noopener" title="https://simple.wikipedia.org/wiki/Turing_complete">Turing complete</a></em>.</p>
<p>Let’s see how you can accomplish this task as a Python one-liner!</p>
<h2>Method 1: Print to Standard Input</h2>
<p>The first method is best if you want to replace all occurrences of <code>"example"</code> with <code>"replaced_example"</code> and print the result to the standard input.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">python -c "print(open('file.txt').read().replace('example','replaced_example'))"</pre>
<p>The replace method replaces all occurrences of the first argument with the second argument. It returns the new string. You can now print the result to the stdin or write it back to a file.</p>
<h2>Method 2: Print to File</h2>
<p>The second method is best if you want to replace all occurrences of <code>"example"</code> with <code>"replaced_example"</code> and write the result to a new file <code>"file2.txt"</code>.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">python -c "print(open('file.txt').read().replace('example','replaced_example'), file=open('file2.txt', 'w'))"</pre>
<p>The replace method replaces all occurrences of the first argument with the second argument. It returns the new string. You can now print the result to the file with means of the <a href="https://blog.finxter.com/the-separator-and-end-arguments-of-the-python-print-function/" title="Python Print Function [And Its SECRET Separator & End Arguments]" target="_blank" rel="noreferrer noopener"><code>file</code> argument of the print function</a>.</p>
<h2>Method 3: exec()</h2>
<p>You can always convert a multi-liner into a one-liner using the exec() function. Say, you have the following multiline script to replace all occurrences of a string in a file:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">with open('file.txt', 'r') as f: s = f.read().replace('example', 'replaced_example')
with open('file.txt', 'w') as f: f.write(s)</pre>
<p>You first open the file in read mode reading all its contents and creating the new string with replaced occurrences of the string <code>'example'</code>. After that, you open the file in writing mode to overwrite its contents.</p>
<p>You use the <code>exec()</code> function to one-linerize this script:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">exec("with open('file.txt', 'r') as f:\n s = f.read().replace('example', 'replaced_example')\nwith open('file.txt', 'w') as f:\n f.write(s)")</pre>
<p>All you did is to replace the new lines with the new line character <code>\n</code>. This resulting script is a not-so-concise one-liner to replace all contents of a given file!</p>
<h2>Where to Go From Here?</h2>
<p>Enough theory, let’s get some practice!</p>
<p>To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?</p>
<p><strong>Practice projects is how you sharpen your saw in coding!</strong></p>
<p>Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?</p>
<p>Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.</p>
<p>Join my free webinar <a rel="noreferrer noopener" href="https://blog.finxter.com/webinar-freelancer/" target="_blank">“How to Build Your High-Income Skill Python”</a> and watch how I grew my coding business online and how you can, too—from the comfort of your own home.</p>
<p><a href="https://blog.finxter.com/webinar-freelancer/" target="_blank" rel="noreferrer noopener">Join the free webinar now!</a></p>
</div>
https://www.sickgaming.net/blog/2020/08/...e-replace/
<div><p><strong>Problem</strong>: You use Python in a terminal and you want to replace a string <code>'example'</code> in a text file <code>file.txt</code>:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">xxxxx example xxxxx</pre>
<p>Your goal is to accomplish the following text:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">xxxxx replaced_example xxxxx</pre>
<p>In particular, you want to open the file, replace the text, and rewrite the result into the file—all in a single line of Python code!</p>
<p><strong><em>Can a <a href="https://blog.finxter.com/python-one-line-x/" target="_blank" rel="noreferrer noopener" title="Python One Line X">Python one-liner</a> accomplish this?</em></strong></p>
<p><strong>Answer</strong>: Yes! You can <a href="https://blog.finxter.com/how-to-execute-multiple-lines-in-a-single-line-python-from-command-line/" title="How to Execute Multiple Lines in a Single Line Python From Command-Line?" target="_blank" rel="noreferrer noopener">compress any Python script</a> into a single line. For computer science nerds: <em>the single line of Python code is <a href="https://simple.wikipedia.org/wiki/Turing_complete" target="_blank" rel="noreferrer noopener" title="https://simple.wikipedia.org/wiki/Turing_complete">Turing complete</a></em>.</p>
<p>Let’s see how you can accomplish this task as a Python one-liner!</p>
<h2>Method 1: Print to Standard Input</h2>
<p>The first method is best if you want to replace all occurrences of <code>"example"</code> with <code>"replaced_example"</code> and print the result to the standard input.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">python -c "print(open('file.txt').read().replace('example','replaced_example'))"</pre>
<p>The replace method replaces all occurrences of the first argument with the second argument. It returns the new string. You can now print the result to the stdin or write it back to a file.</p>
<h2>Method 2: Print to File</h2>
<p>The second method is best if you want to replace all occurrences of <code>"example"</code> with <code>"replaced_example"</code> and write the result to a new file <code>"file2.txt"</code>.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">python -c "print(open('file.txt').read().replace('example','replaced_example'), file=open('file2.txt', 'w'))"</pre>
<p>The replace method replaces all occurrences of the first argument with the second argument. It returns the new string. You can now print the result to the file with means of the <a href="https://blog.finxter.com/the-separator-and-end-arguments-of-the-python-print-function/" title="Python Print Function [And Its SECRET Separator & End Arguments]" target="_blank" rel="noreferrer noopener"><code>file</code> argument of the print function</a>.</p>
<h2>Method 3: exec()</h2>
<p>You can always convert a multi-liner into a one-liner using the exec() function. Say, you have the following multiline script to replace all occurrences of a string in a file:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">with open('file.txt', 'r') as f: s = f.read().replace('example', 'replaced_example')
with open('file.txt', 'w') as f: f.write(s)</pre>
<p>You first open the file in read mode reading all its contents and creating the new string with replaced occurrences of the string <code>'example'</code>. After that, you open the file in writing mode to overwrite its contents.</p>
<p>You use the <code>exec()</code> function to one-linerize this script:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">exec("with open('file.txt', 'r') as f:\n s = f.read().replace('example', 'replaced_example')\nwith open('file.txt', 'w') as f:\n f.write(s)")</pre>
<p>All you did is to replace the new lines with the new line character <code>\n</code>. This resulting script is a not-so-concise one-liner to replace all contents of a given file!</p>
<h2>Where to Go From Here?</h2>
<p>Enough theory, let’s get some practice!</p>
<p>To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?</p>
<p><strong>Practice projects is how you sharpen your saw in coding!</strong></p>
<p>Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?</p>
<p>Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.</p>
<p>Join my free webinar <a rel="noreferrer noopener" href="https://blog.finxter.com/webinar-freelancer/" target="_blank">“How to Build Your High-Income Skill Python”</a> and watch how I grew my coding business online and how you can, too—from the comfort of your own home.</p>
<p><a href="https://blog.finxter.com/webinar-freelancer/" target="_blank" rel="noreferrer noopener">Join the free webinar now!</a></p>
</div>
https://www.sickgaming.net/blog/2020/08/...e-replace/