Removing white space and colon
<p>I have a file with a bunch of numbers that have white spaces and colons and I am trying to remove them. As I have seen on this forum the function <code>line.strip.split()</code> works well to achieve this. Is there a way of removing the white space and colon all in one go? Using the method posted by Lorenzo I have this:</p>

<pre><code>train = []
with open('C:/Users/Morgan Weiss/Desktop/STA5635/DataSets/dexter/dexter_train.data') as train_data:
    train.append(train_data.read().replace(' ','').replace(':',''))
size_of_train = np.shape(train)
for i in range(size_of_train[0]):
    for j in range(size_of_train[1]):
        train[i][j] = int(train[i][j])
print(train)
</code></pre>

<p>Although I get this error:</p>

<pre class="lang-none prettyprint-override"><code>File "C:/Users/Morgan Weiss/Desktop/STA5635/Homework/Homework_1/HW1_Dexter.py", line 11, in &lt;module&gt;
    for j in range(size_of_train[1]):
IndexError: tuple index out of range
</code></pre>

<p>You did not provide an example of what your input file looks like so we can only speculate what solution you need. I'm going to suppose that you need to extract integers from your input text file and print their values.</p>

<p>Here's how I would do it:</p>

<ul>
<li>Instead of trying to eliminate whitespace characters and colons, I will be searching for digits using a <a href="https://docs.python.org/3/library/re.html" rel="nofollow">regular expression</a></li>
<li>Consecutive digits would constitute a number</li>
<li>I would convert this number to an integer form.</li>
</ul>

<p>And here's how it would look like:</p>

<pre><code>import re

input_filename = "/home/evens/Temporaire/Stack Exchange/StackOverflow/Input_file-39359816.txt"

matcher = re.compile(r"\d+")

with open(input_filename) as input_file:
    for line in input_file:
        for digits_found in matcher.finditer(line):
            number_in_string_form = digits_found.group()
            number = int(number_in_string_form)
            print(number)
</code></pre>

<p>But before you run away with this code, you should continue to learn Python because you don't seem to grasp its basic elements yet.</p>

python numpy