输出中未反映代码更改

2024-04-29 07:47:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我在更新代码时遇到一些问题。我可以运行我的代码,它运行良好。但是当我对代码进行调整时,输出与我最初运行该文件时保持不变。如果我创建了一个新的.py文件,然后复制并粘贴更新后的代码,那么它将生成所需的带有更新的输出。为什么我的原始文件没有反映输出中的更改?在

我的具体例子在下面的代码中。代码按预期运行并生成输出。然后我更新了它,添加了“sector”和“close”变量。但是,新的输出不包括数据,只包括标题中的名称。和pyc文件有关吗?在

import multiprocessing
import datetime
import re
from progressbar import ProgressBar
import csv
import urllib2
from lxml import etree

def mp_worker(s):
    url1 = ("https://research.tdameritrade.com/grid/public/research/stocks/fundamentals?symbol=" + s)
    url2 = ("https://research.tdameritrade.com/grid/public/research/stocks/summary?symbol=" + s)
    url3 = ("https://research.tdameritrade.com/grid/public/research/stocks/industryposition?symbol=" + s)
    htmlparser = etree.HTMLParser()
    try:
        response3 = urllib2.urlopen(url3)
        tree3 = etree.parse(response3, htmlparser)
        perf = tree3.xpath("""//*[@id="stock-industrypositionmodule"]/div/div/table/tbody[1]/tr[3]/td[1]/text()""")

        if len(perf) > 0:
            EPS5yr = tree3.xpath("""//*[@id="stock-industrypositionmodule"]/div/div/table/tbody[2]/tr[4]/td[1]/text()""")
        else:
            response1 = urllib2.urlopen(url1)
            tree1 = etree.parse(response1, htmlparser)
            EPS5yr = tree1.xpath("""//*[@id="layout-full"]/div[3]/div/div[3]/section/div/div/div[1]/div/dl/dd[1]/div/label/span/text()""")

        response2 = urllib2.urlopen(url2)
        tree2 = etree.parse(response2, htmlparser)
        EPSttm = tree2.xpath("""//*[@id="stock-summarymodule"]/div/div/div[1]/div/div[2]/dl/ul/li[3]/dd/text()""")
        sector = tree2.xpath("""//*[@id="layout-header"]/div[1]/div/text()""")
        indy = tree2.xpath("""//*[@id="layout-header"]/div[1]/div/a[1]/text()""")
        close = tree2.xpath("""//*[@id="stock-quotebar"]/div/div/table/tbody/tr/td[1]/dl/dd/text()""")

    except Exception as e:
        EPS5yr = 'Error'
        EPSttm = 'Error'
        perf = 'Error'
        indy = 'Error'
        close = 'Error'
        sector = 'Error'
        pass

    return s, close, EPS5yr, EPSttm, perf, sector, indy

def mp_handler():
    now = datetime.datetime.now()
    date = now.strftime("%Y-%m-%d_%H%M")
    file = ('total_market' + '_' + date +'.csv')
    p = multiprocessing.Pool(16)
    symbols = {'AABA',
    'AAOI',
    'AAPL',
    'AAWC'}
    with open(file, "ab") as csv_file:
        writer = csv.writer(csv_file, delimiter=',')
        writer.writerow(['Symbol','Price','5 Yr EPS','EPS TTM','52 Wk Perf','Sector','Industry'])
        for result in p.imap(mp_worker, symbols):
            # (filename, count) tuples from worker
            writer.writerow(result)

if __name__=='__main__':
    mp_handler()

Tags: 文件csv代码textimportdividclose