从输出中以Python保存文件

时间:2018-08-22 17:51:45

标签: python python-3.x python-2.7

这里有您的问题。我有这个用于python的脚本,用于检查大型数据集中的电子邮件并提取它们。在我的Mac上,它仅显示终端中的所有电子邮件地址。有时文件是1-2个演出,因此可能要花一点时间,并且输出是疯狂的。我想知道在Python中将其保存到文件而不是在终端中全部打印出来是多么容易。

我什至不需要看到所有东西都被倒入终端中。

这是我正在使用的脚本

#!/usr/bin/env python
#
# Extracts email addresses from one or more plain text files.
#
# Notes:
# - Does not save to file (pipe the output to a file if you want it saved).
# - Does not check for duplicates (which can easily be done in the terminal).
#


from optparse import OptionParser
import os.path
import re

regex = re.compile(("([a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_`"
                    "{|}~-]+)*(@|\sat\s)(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?(\.|"
                    "\sdot\s))+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)"))

def file_to_str(filename):
    """Returns the contents of filename as a string."""
    with open(filename) as f:
        return f.read().lower() # Case is lowered to prevent regex mismatches.

def get_emails(s):
    """Returns an iterator of matched emails found in string s."""
    # Removing lines that start with '//' because the regular expression
    # mistakenly matches patterns like 'http://foo@bar.com' as '//foo@bar.com'.
    return (email[0] for email in re.findall(regex, s) if not email[0].startswith('//'))

if __name__ == '__main__':
    parser = OptionParser(usage="Usage: python %prog [FILE]...")
    # No options added yet. Add them here if you ever need them.
    options, args = parser.parse_args()

    if not args:
        parser.print_usage()
        exit(1)

    for arg in args:
        if os.path.isfile(arg):
            for email in get_emails(file_to_str(arg)):
                print email
        else:
            print '"{}" is not a file.'.format(arg)
            parser.print_usage()

3 个答案:

答案 0 :(得分:0)

首先,您需要打开一个文件: file = open('output', 'w')

然后,不要打印电子邮件,而是将其写入文件file.write(email + '\n')

您也可以按照jasonharper所说的在执行时将程序输出重定向到文件。

答案 1 :(得分:0)

代替打印,只需写入文件即可。

with open('filename.txt', 'w') as f: f.write('{}\n'.format(email))

答案 2 :(得分:0)

打印时,用write语句替换

    for arg in args:
    if os.path.isfile(arg):
        for email in get_emails(file_to_str(arg)):
            print email

在那,只需替换为

    for arg in args:
    if os.path.isfile(arg):
        for email in get_emails(file_to_str(arg)):
            with open (tempfile , 'a+') as writefile:
                writefile.write(name+'\n')

tempfile是输出文件的位置