Add a character on a list if a situation presents
<p>I have this script:</p>

<pre><code>accounts = open("accounts.txt").readlines()

y = [x.strip().split(":") for x in accounts]

for position, account in enumerate(y):
    try:
        print ("Trying with: %s:%s @%d" % (account[0], account[1], position))
    except:
        pass
</code></pre>

<p>it opens a file (accounts.txt) that is structured like this:</p>

<pre><code>email1@email.com:test1
email2@email.com:test2
email3@email.comtest3
email4@email.comtest4
</code></pre>

<p>Since I want to split the email and the password, I'd like, if the try doesn't work (so the ":" is not in the line of the file (and account[1] doesn't exist)), to add the ":" after the ".com" of each email on the file. Is that possible? </p>

<p>The output of the third and fourth accounts should be:</p>

<pre><code>email3@email.com:test3
email4@email.com:test4
</code></pre>

<p>You could add a <code>correct_accounts</code> function to your code.</p>

<pre><code>def correct_accounts(accounts):
    corr = []
    for x in accounts:
        if ':' not in x:
            # Assuming all mail addresses end with .com
            pos = x.find ('.com') + 4
            corr.append(x [:pos] + ':' + x [pos:])
        else:
            corr.append(x)
    return corr
</code></pre>

<p>and call:</p>

<pre><code>y = [x.strip().split(":") for x in correct_accounts(accounts)]
</code></pre>

python list python-3.x for-loop split