将元组数据写入txt文件中
2016-11-29 10:18
363 查看
利用urllib库获取雅虎财经数据,得到元组数据
考虑将数据写入到txt文件当中,使用如下代码:
能得到
AAPL, 蘋果公司, 111.570
AXP, American Express Company, 72.130
BA, The Boeing Company, 149.770
CAT, Caterpillar Inc., 94.900
CSCO, 思科系統公司, 29.930
CVX, Chevron Corporation, 110.500
DD, E. I. du Pont de Nemours and Company, 70.890
DIS, The Walt Disney Company, 98.970
GE, General Electric Company, 31.250
GS, The Goldman Sachs Group, Inc., 210.350
HD, The Home Depot, Inc., 130.640
IBM, International Business Machines Corporation, 164.520
INTC, 英特爾公司, 35.510
JNJ, Johnson & Johnson, 113.130
JPM, JPMorgan Chase & Co., 78.320
KO, The Coca-Cola Company, 41.750
MCD, McDonald’s Corporation, 121.820
MMM, 3M Company, 172.500
MRK, Merck & Co., Inc., 61.800
MSFT, 微軟公司, 60.610
NKE, NIKE, Inc., 51.010
PFE, Pfizer Inc., 31.540
PG, The Procter & Gamble Company, 83.070
TRV, The Travelers Companies, Inc., 113.810
UNH, UnitedHealth Group Incorporated, 152.110
UTX, United Technologies Corporation, 108.450
V, Visa Inc., 79.080
VZ, Verizon Communications Inc., 51.120
WMT, Wal-Mart Stores Inc., 71.190
XOM, Exxon Mobil Corporation, 86.470
import urllib2 import re dStr = urllib2.urlopen('https://hk.finance.yahoo.com/q/cp?s=%5EDJI').read() m = re.findall('<tr><td class="yfnc_tabledata1"><b><a href=".*?">(.*?)</a></b></td><td class="yfnc_tabledata1">(.*?)</td>.*?<b>(.*?)</b>.*?</tr>', dStr)
考虑将数据写入到txt文件当中,使用如下代码:
with open('D:\pythoncode\data\yahoofinanicaldata.txt', 'w') as graphd: for row in m: print >>graphd, ', '.join(map(str, row)) graphd.close()
能得到
AAPL, 蘋果公司, 111.570
AXP, American Express Company, 72.130
BA, The Boeing Company, 149.770
CAT, Caterpillar Inc., 94.900
CSCO, 思科系統公司, 29.930
CVX, Chevron Corporation, 110.500
DD, E. I. du Pont de Nemours and Company, 70.890
DIS, The Walt Disney Company, 98.970
GE, General Electric Company, 31.250
GS, The Goldman Sachs Group, Inc., 210.350
HD, The Home Depot, Inc., 130.640
IBM, International Business Machines Corporation, 164.520
INTC, 英特爾公司, 35.510
JNJ, Johnson & Johnson, 113.130
JPM, JPMorgan Chase & Co., 78.320
KO, The Coca-Cola Company, 41.750
MCD, McDonald’s Corporation, 121.820
MMM, 3M Company, 172.500
MRK, Merck & Co., Inc., 61.800
MSFT, 微軟公司, 60.610
NKE, NIKE, Inc., 51.010
PFE, Pfizer Inc., 31.540
PG, The Procter & Gamble Company, 83.070
TRV, The Travelers Companies, Inc., 113.810
UNH, UnitedHealth Group Incorporated, 152.110
UTX, United Technologies Corporation, 108.450
V, Visa Inc., 79.080
VZ, Verizon Communications Inc., 51.120
WMT, Wal-Mart Stores Inc., 71.190
XOM, Exxon Mobil Corporation, 86.470
相关文章推荐
- 1000条数据写入到txt文件中,并且做了换行
- ORACLE 中写入txt文本与从Txt文件中读入数据 修改表结构
- 将数据写入TXT文件中,file_put_contents与fwrite
- java读取Excel数据,然后写入到txt文件,并批量保存到oracle数据库中
- java向txt文件中写入数据时换行
- 读取Excel数据和写入txt文件以及读取配置文件工具类
- Java:对文件A.txt内部数据逐行排序,并写入B.txt
- vc++向txt文件中写入数据,追加数据
- 用MATLAB将矩阵数据写入txt文件中,打开乱码原因
- 取出数据写入xml,txt文件
- j2me 创建txt文件,写入数据
- TXT创建写入,每个文件允许1W条数据。超过1W条数据分割成多个文件
- 第十七章,txt文件的写入和读取数据结合练习(C++)
- php写入数据到txt文件
- ORACLE 中写入txt文本与从Txt文件中读入数据 修改表结构
- VB.NET 读取INI,TXT文件 写入数据
- 创建txt文件并写入数据和获取当前的系统时间方式
- asp.net(c#)如何读取上传过程中的.txt文件中的数据,并将其写入数据库的
- 《程序实现》从xml、txt文件里读取数据写入excel表格
- 将数据写入.txt文件中