Python循环结果为CSV时

时间:2017-11-04 23:43:43

标签: python-2.7

我的目标是获取股票名称(代码)和股票价格。我能够打印结果,但我不知道如何将这些结果保存到text / csv文件。我的symbols.txt文件中的值为:

PIH 
TURN
FLWS
FCCY
SRCE
VNET
TWOU

我目前运行的脚本是:

import urllib
import re

symbolfile = open("symbols.txt")

symbolslist = symbolfile.read()

symbolslist = symbolslist.split("\n")

i=0
while i<len(symbolslist):
    url = "http://www.nasdaq.com/symbol/" +symbolslist[i]
    htmlfile = urllib.urlopen(url)
    htmltext = htmlfile.read()
    regex = '<div id="qwidget_lastsale" class="qwidget-dollar">(.+?)</div>'
    pattern = re.compile(regex)
    price = re.findall(pattern,htmltext)
    print "the price of",symbolslist[i]," is " ,price
    i+=1

我目前的输出是:

the price of PIH  is  ['$7.175']
the price of TURN  is  ['$2.03']
the price of FLWS  is  ['$9.45']
the price of FCCY  is  ['$18']
the price of SRCE  is  ['$50.87']
the price of VNET  is  ['$7.145']
the price of TWOU  is  ['$63.89']

我想要的输出是text / csv文件,其中包含以下值:

PIH,$7.175
TURN,$2.03
FLWS,$9.45
FCCY,$18
SRCE,$50.87
VNET,$7.145
TWOU,$63.89

美元符号,括号等都可以。我可以删除其他程序中的那些。提前谢谢!

1 个答案:

答案 0 :(得分:1)

试试这个:

import urllib
import re

symbolfile = open("symbols.txt")

symbolslist = symbolfile.read()

symbolslist = symbolslist.split("\n")

with open('output.csv', 'w') as output:
    i=0
    while i<len(symbolslist):
        url = "http://www.nasdaq.com/symbol/" +symbolslist[i]
        htmlfile = urllib.urlopen(url)
        htmltext = htmlfile.read()
        regex = '<div id="qwidget_lastsale" class="qwidget-dollar">(.+?)</div>'
        pattern = re.compile(regex)
        price = re.findall(pattern,htmltext)
        output.write('%s,%s\n'%(symbolslist[i], price[0]))
        print "the price of",symbolslist[i]," is " ,price
        i+=1