您的位置:首页 > 编程语言 > Python开发

将元组数据写入txt文件中

2016-11-29 10:18 363 查看
利用urllib库获取雅虎财经数据,得到元组数据

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Python