简体   繁体   English

将结果保存到 txt 文件 python

[英]Save results to a txt file python

I want to save these email results to my results.txt file in the directory.我想将这些电子邮件结果保存到目录中的 results.txt 文件中。

def parseAddress():
    try:
        website = urllib2.urlopen(getAddress())
        html = website.read()

        addys = re.findall('''[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?''', html, flags=re.IGNORECASE)

        print addys

    except urllib2.HTTPError, err:
        print "Cannot retrieve URL: HTTP Error Code: ", err.code
    except urllib2.URLError, err:
        print "Cannot retrive URL: " + err.reason[1]

    # need to write the addys data to results.txt   
    with open('results.txt', 'w') as f:
       result_line = f.writelines(addys)

You mistakenly indented the "with" statement one space.您错误地将“with”语句缩进了一个空格。 This makes it subjective to an earlier block.这使得它对较早的块具有主观性。 I would think any self-respecting Python interpreter would flag this as not matching any earlier indentation, but it seems to be fouling your output.我认为任何有自尊的 Python 解释器都会将此标记为与任何早期的缩进不匹配,但它似乎污染了您的输出。

Also, please consider adding some tracing print statements to see where your code did execute.另外,请考虑增加一些跟踪打印语句,看看你的代码执行。 That output alone can often show you the problem, or lead us to it.该输出本身通常可以向您展示问题,或引导我们解决问题。 You should always provide actual output for us, rather than just a general description.您应该始终为我们提供实际输出,而不仅仅是一般描述。

Use return addys at the end of your function.在函数末尾使用return addys print will only output to your screen. print只会输出到您的屏幕。

In order to retrieve addys , you would need to call the function in your with statement or create a variable that contains the result of parseAddress() .为了检索addys ,您需要在with语句中调用该函数或创建一个包含parseAddress()结果的变量。

You can save the memory that a variable would use by simply calling the function, like so:您可以通过简单地调用函数来节省变量将使用的内存,如下所示:

with open('results.txt', 'w') as f:
    f.write ( parseAddress() )

You need to fix your indentation, which is important in Python as it is the only way to define a block of code.您需要修复缩进,这在 Python 中很重要,因为它是定义代码块的唯一方法。

You also have too many statements in your try block.您的try块中的语句也太多。

def parseAddress():
    website = None
    try:
        website = urllib2.urlopen(getAddress())
    except urllib2.HTTPError, err:
        print "Cannot retrieve URL: HTTP Error Code: ", err.code
    except urllib2.URLError, err:
        print "Cannot retrive URL: " + err.reason[1]

    if website is not None:
        html = website.read()
        addys = re.findall('''[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?''', html, flags=re.IGNORECASE)
        print addys

        # need to write the addys data to results.txt   
        with open('results.txt', 'w') as f:
            result_line = f.writelines(addys)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM