我在更新代码方面遇到了一些问题。我可以运行我的代码,它工作正常。但是当我对代码进行调整时,输出保持与我最初运行文件时相同。如果我创建一个新的.py文件,只是复制并粘贴更新的代码,它会生成带有更新的所需输出。为什么我的原始文件没有反映输出中的更改?
我的具体示例在下面的代码中。代码按预期运行并生成输出。然后我更新它以添加“扇区”和“关闭”变量。但是,新输出不包括数据,只包括标题中的名称。是否与.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()
答案 0 :(得分:0)
尽管这是一个古老的问题,但我想提供另一个可能的解决方案。如果该模块是您安装的东西(例如,您有setup.py文件并且已pip安装了项目),则卸载该模块可以解决该问题。事实证明,这可以解决我遇到的问题。