Hi guys,

My university recently switched mail services and in order to maintain our new listservs we have to import the emails using a different format. I have all the emails in a text file like this:

test@testing.com
test2@testing.com
test3@testing.com

In order to import it to the new mailing system it has to be in this format:

<test@testing.com> ""
<test2@testing.com>""
<test3@test.com>""

since there are lot of records I don't want to go through and edit each individually. I can import the text file, but just cannot add the characters to the list, I have tried li.append, but can't figure out why it will not work. Any help would be greatly appreciated!

thanks!

Dani AI

Generated

Quick diagnosis for (and thanks to for asking for the code): the posted function never actually opens the filename (an undefined name is used), iterates raw lines that still contain newline characters, and concatenates the wrapper incorrectly. A reliable, simple approach is to open the input with a context manager, strip whitespace from each line, remove any existing angle brackets or stray quotes, skip empty lines, then write each address in the required wrapper to an output file.

#!/usr/bin/env python3
import sys

def transform(line):
    addr = line.strip()
    if not addr:
        return None
    addr = addr.strip('<> \'"')
    return '<' + addr + '> ""'

def process(inpath, outpath):
    with open(inpath, 'r', encoding='utf-8') as src, open(outpath, 'w', encoding='utf-8') as dst:
        for line in src:
            out = transform(line)
            if out:
                dst.write(out + '\n')

if __name__ == '__main__':
    if len(sys.argv) != 3:
        print('Usage: script.py input.txt output.txt')
        sys.exit(1)
    process(sys.argv[1], sys.argv[2])

The code uses a context manager so files are always closed, strip() removes newline/space, and strip('<> \'"') cleans up any preexisting brackets or quotes. Run it as python3 script.py input.txt output.txt. Common pitfalls: test on a copy before overwriting originals, watch for Windows CRLF (use line.rstrip('\r\n') if needed), and deduplicate addresses later if the import requires uniqueness (an OrderedDict or dict.fromkeys() preserves order while removing duplicates).

Recommended Answers

All 2 Replies

Hi there,
Welcome to daniweb. Please post down the code that you have written :). So that we could explore upon whats going on :).

oh oops my mistake.

this is what I have so far:

import sys

def maillist(file1):
    f1 = (openfile1)
    """Return new standardized email list"""
    new_list = []
    for value in f1:
        new_email = "<"+value[0:]+">"+"''"
        new_list.append(new_email)
    return new_list

f1 = (openfile1)
print (f1)
f1 = maillist(f1)
print (f1)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.