Splitting one line into multiple ones given a separator I have a file containing one long line:

"name surname" &lt;name.surname@example.com&gt;, 'name surname' &lt;name.surname@example.com&gt;, name surname &lt;name.surname@example.com&gt;, "'name surname'" &lt;name.surname@example.com&gt;, surname, &lt;name.surname@example.com&gt;, name &lt;name.surname@example.com&gt;


Note that that's 6 different forms.

I am splitting each email address into its own line, and saving the results into another file:

import sys

ifile = sys.argv[1]
ofile = sys.argv[2]

with open(ifile) as ifile, open(ofile, "w") as ofile:
    addresses = ifile.readline().split("&gt;,")
    for n, address in enumerate(addresses):
        address = address.replace("'", "")
        address = address.replace('"', "")
        name, address = address.split("&lt;")
        address = "&lt;" + address
        if len(name) &gt; 1:
            name = name.strip()
            name = '"{}" '.format(name)
            address = "".join(name + address)
        if n &lt; len(addresses) - 1:
            ofile.write(address.strip() + "&gt;\n")
        else:
            ofile.write(address.strip() + "\n")


Feels to me like hackery so am looking for a better solution.
Why are you first removing the quotes and then putting them back?

And why are you removing the brackets and them putting them back?

This does the same thing, except change ' to ". It also doesn't handle commas in names,
so if you have that it won't work. In that case I'd probably use a regexp.

import sys

ifile = sys.argv[1]
ofile = sys.argv[2]

with open(ifile) as ifile, open(ofile, "w") as ofile:
    for address in ifile.readline().split(","):
        ofile.write(address.strip() + '\n')


Update:

"surname, name &lt;name.surname@example.com&gt;" sucks, and that means your format is inconsistent and not parseable without horrid hacks. In that case your code seems OK, although I'd probably do it differently. I would most likely use a regexp to find all cases of commas that are NOT preceded by > and followed by a space to something else, say chr(128) or something like that. I'd then parse the code with my code above, extract the email from withing the brackets, strip all quotes and brackets from the remander, and replace back chr(128) with commas.

And the lastly write that to the outfile.

The difference there is that I don't try to handle a horrid format, I first try to fix the problems. It makes for cleaner code, IMO.

Update 2:

I instead replaced the commas that should be split on, making it simpler, like so:

import sys

ifile = sys.argv[1]
ofile = sys.argv[2]

with open(ifile) as ifile, open(ofile, "w") as ofile:
    data = ifile.read()
    data = data.replace('&gt;,', '&gt;\xF0')
    for line in data.split('\xF0'):
        name, email = line.split('&lt;')
        email = email.replace('&gt;', '').strip()
        name = name.replace('"', '').replace("'", "").strip()
        ofile.write('"%s" &lt;%s&gt;\n' % (name, email))


and then I realized I could simplify it even more:

import sys

ifile = sys.argv[1]
ofile = sys.argv[2]

with open(ifile) as ifile, open(ofile, "w") as ofile:
    data = ifile.read()
    for line in data.split('&gt;,'):
        name, email = line.split('&lt;')
        email = email.strip()
        name = name.replace('"', '').replace("'", "").strip()
        ofile.write('"%s" &lt;%s&gt;\n' % (name, email))


And as this point I'm basically doing what you are doing, but much simplified.