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>I think the above syntax is not correct, but anyways as per your question, you can use replace function present in python.</p>

<p>When reading each line as a string from that file you can do something like, </p>

<pre><code>train = []
with open('/Users/sushant.moon/Downloads/dexter_train.data') as f:
    list = f.read().split()
    for x in list:
        data = x.split(':')
        train.append([int(data[0]),int(data[1])])

# this part becomes redundant as i have already converted str to int before i append data to train
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])
</code></pre>

<p>Here I am using replace function to replace space with blank string, and similar with colon.</p>

python numpy