Python - 将多个列表写入文件(Python - Write multiple lists to file)
我正在编写股票代码程序,它会将结果显示在屏幕上并将其写入文件。 我将结果显示在屏幕上没有问题,但文件中的结果并不符合我的预期。
将结果显示在屏幕上的代码:
tickerList = ticker.split() quotes = get_stock_quote(tickerList) for quote in quotes: print 'ticker: %s' % quote['t'], 'current price: %s' % quote['l_cur'], 'last trade: %s' % quote['lt']
结果(如我所料):
ticker: AAPL current price: 111.31 last trade: Oct 6, 4:00PM EDT ticker: GOOG current price: 645.44 last trade: Oct 6, 4:00PM EDT ticker: IBM current price: 148.80 last trade: Oct 6, 6:20PM EDT
要写入文件的代码:
for quote in quotes: out_quotes = ['ticker: %s ' % quote['t'], 'current price: %s ' % quote['l_cur'], 'last trade: %s ' % quote['lt']] outfile = open('result.txt', 'w') for quote in out_quotes: outfile.writelines(chain(*out_quotes)) outfile.write('\n')
结果:
ticker: IBM current price: 148.80 last trade: Oct 6, 6:20PM EDT ticker: IBM current price: 148.80 last trade: Oct 6, 6:20PM EDT ticker: IBM current price: 148.80 last trade: Oct 6, 6:20PM EDT
我期待与显示在屏幕上的结果相同的结果。 任何人都可以帮助指出我的错误? 提前致谢。
I'm writing stock ticker program that will display the result onto the screen and also write it to file. I had no problem with display the result onto the screen, but the result in the file was not what I expected.
Code to display the result to the screen:
tickerList = ticker.split() quotes = get_stock_quote(tickerList) for quote in quotes: print 'ticker: %s' % quote['t'], 'current price: %s' % quote['l_cur'], 'last trade: %s' % quote['lt']
Result (as I expected):
ticker: AAPL current price: 111.31 last trade: Oct 6, 4:00PM EDT ticker: GOOG current price: 645.44 last trade: Oct 6, 4:00PM EDT ticker: IBM current price: 148.80 last trade: Oct 6, 6:20PM EDT
Code to write to file:
for quote in quotes: out_quotes = ['ticker: %s ' % quote['t'], 'current price: %s ' % quote['l_cur'], 'last trade: %s ' % quote['lt']] outfile = open('result.txt', 'w') for quote in out_quotes: outfile.writelines(chain(*out_quotes)) outfile.write('\n')
Result :
ticker: IBM current price: 148.80 last trade: Oct 6, 6:20PM EDT ticker: IBM current price: 148.80 last trade: Oct 6, 6:20PM EDT ticker: IBM current price: 148.80 last trade: Oct 6, 6:20PM EDT
I was expecting the same result as the one displayed onto the screen. Anyone can help to point out my mistake? Thanks in advance.
原文:https://stackoverflow.com/questions/33004908
相关文章
更多探索 Python,第 1 部分: Python 的内置数值类型
Java 流(Stream)、文件(File)和IO
Solr4.7.2启动时的Index locked for write for core问题分析
Python内建函数(F)
python下载pps视频
python top project of 2013
Python:渗透测试开源项目【源码值得精读】
python2和python3的区别
采用Python来访问Hadoop HSFS存储实现文件的操作
Python资源索引 【转载】
最新问答
更多获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
如何通过引用返回对象?(How is returning an object by reference possible?)
矩阵如何存储在内存中?(How are matrices stored in memory?)
每个请求的Java新会话?(Java New Session For Each Request?)
css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
xcode语法颜色编码解释?(xcode syntax color coding explained?)
在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
Copyright ©2023 peixunduo.com All Rights Reserved.粤ICP备14003112号
本站部分内容来源于互联网,仅供学习和参考使用,请莫用于商业用途。如有侵犯你的版权,请联系我们(neng862121861#163.com),本站将尽快处理。谢谢合作!