python 2.7:追加日志返回csv

时间:2014-01-13 14:50:38

标签: python python-2.7 csv logging append

使用以下代码,我每隔5分钟尝试从网站获取数据,为其设置时间戳,计算其logn返回值并将所有内容附加到csv文件中。

抓取数据,加盖时间戳并附加到csv可以正常工作,但是当我试图弄清楚如何包含日志返回时,我有点卡住了。

import time
from time import strftime, gmtime
import numpy as np
import urllib2
from urllib2 import urlopen
from math import log

coiAr = []
dateAr = []
logReAr = []

def mcapGrab():
    while True:
        try:
            sourceCode = urllib2.urlopen('http://coinmarketcap.com').read() 

            mcapUSD = sourceCode.split('<strong>Total Market Cap: <span id="total-marketcap" data-usd="')[1].split('"')[0]
            coiAr.append(float(mcapUSD.replace(',','')))

            date = strftime('%d %b %Y %H:%M:%S', gmtime())
            dateAr.append(date)         
#           if len(coiAr) > 0:
#               indexLog = 1
#               logRe = log(coiAr[indexLog]/coiAr[indexLog-1])
#               logReAr.append(logRe)
#               indexLog += 1
#           else:
#               logReAr.append(0)

            for eachMcap in coiAr:
                saveLine = date+','+str(eachMcap)+'\n'
                saveFile = open('mcapdata.csv', 'a')
                saveFile.write(saveLine)
                saveFile.close()

                s = 0
                print dateAr[s]+','+str(coiAr[s])
                time.sleep(300)
                s+= 1


        except Exception, e: 
            print 'Failed to grab market cap', str(e)

mcapGrab()

我已经注释掉了我尝试计算并添加日志返回的部分,但是没有用。

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

不要使用全局列表;只需在找到时将每个条目写入文件即可。使用csv模块可以使这一切变得更加容易:

import csv

sourceCode = urllib2.urlopen('http://coinmarketcap.com').read() 
mcapUSD = sourceCode.split('<strong>Total Market Cap: <span id="total-marketcap" data-usd="')[1].split('"')[0]
mcap = float(mcapUSD.replace(',','')

# read previous value from the CSV first
with open('mcapdata.csv', 'rb') as infh:
    last = None
    for row in csv.reader(infh):
        last = row[1]  # second column

# Now calculate the log value based on the last value
logRe = log(mcap/float(last))

# write new values
with open(mcapdata.csv', 'ab') as outfh:
    date = strftime('%d %b %Y %H:%M:%S', gmtime())
    csv.writer(outfh).writerow([date, mcap, logRe])

此代码将读取mcapdata.csv文件,只选择写入的最后一个值。您也可以将所有行保留在内存中,只选择列表列表中的最后一个条目。