Reading a textfile into a String
<p>I'm just starting to learn python and have a  textfile that looks like this:</p>

<pre><code>Hello

World


Hello
World
</code></pre>

<p>And I want to add the numbers '55' to the beggining and end of every string that starts with 'hello'</p>

<p>The numbers '66' to the beggining and every of every string that starts with 'World'</p>

<p>etc</p>

<p>So my final file should look like this:</p>

<pre><code>55Hello55

66World66


55Hello55
66World66
</code></pre>

<p>I'm reading the file in all at once, storing it in a string, and then trying to append accordingly</p>

<pre><code>fp = open("test.txt","r")
strHolder = fp.read()
print(strHolder)

if 'Hello' in strHolder:
    strHolder = '55' + strHolder + '55'
if 'World' in strHolder:
    strHolder = '66' + strHolder + '66'
print(strHolder)
fp.close()
</code></pre>

<p>However, my string values '55' and '66' are always being added to the front of the file and end of the file, not the front of a certain string and to the end of the string, where I get this output of the string:</p>

<pre><code>6655Hello

World


Hello
World
5566
</code></pre>

<p>Any help would be much appreciated.</p>

<p>To read a <strong>text</strong> file, I recommend the following way which is compatible with Python 2 &amp; 3:</p>

<pre><code>import io

with io.open("test", mode="r", encoding="utf8") as fd:
    ...
</code></pre>

<p>Here, I make the assumption that your file use uft8 encoding.</p>

<p>Using a <code>with</code> statement make sure the file is closed at the end of reading even if a error occurs (an exception). To learn more about context manager, take a look at the <a href="https://docs.python.org/2/library/contextlib.html" rel="nofollow">Context Library</a>. </p>

<p>There are several ways to read a text file:</p>

<ul>
<li>read the whole file with: <code>fd.read()</code>, or</li>
<li>read line by line with a loop: <code>for line in fd</code>.</li>
</ul>

<p>If you read the whole file, you'll need to split the lines (see <a href="https://docs.python.org/2/library/stdtypes.html?highlight=splitlines#str.splitlines" rel="nofollow">str.splitlines</a>. Here are the two solutions:</p>

<pre><code>with io.open("test", mode="r", encoding="utf8") as fd:
    content = fd.read()
for line in content.splilines():
    if "Hello" in line:
        print("55" + line + "55")
    if "World" in line:
        print("66" + line + "66")
</code></pre>

<p>Or</p>

<pre><code>with io.open("test", mode="r", encoding="utf8") as fd:
    for line in content.splilines():
        line = line[:-1]
        if "Hello" in line:
            print("55" + line + "55")
        if "World" in line:
            print("66" + line + "66")
</code></pre>

<p>If you need to write the result in another file you can open the output file in write mode and use <code>print(thing, file=out)</code> as follow:</p>

<pre><code>with io.open("test", mode="r", encoding="utf8") as fd:
    with io.open("test", mode="w", encoding="utf8") as out:
        for line in content.splilines():
            line = line[:-1]
            if "Hello" in line:
                print("55" + line + "55", file=out)
            if "World" in line:
                print("66" + line + "66", file=out)
</code></pre>

<p>If you use Python 2, you'll need the following directive to use the print function:</p>

<pre><code>from __future__ import print_function
</code></pre>

python