Python:在csv

时间:2018-02-03 14:23:48

标签: python pandas csv

我需要在csv中添加第一行列。下面是我获取csv的代码

import urllib,time,datetime
import pandas as pd


class Quote(object):

  DATE_FMT = '%Y-%m-%d'
  TIME_FMT = '%H:%M:%S'

  def __init__(self):
    self.symbol = ''
    #self.date,self.time,self.open_,self.high,self.low,self.close,self.volume = ([] for _ in range(7))
    self.date,self.open_,self.high,self.low,self.close,self.volume = ([] for _ in range(6))

  def append(self,dt,open_,high,low,close,volume):
    self.date.append(dt.date())
    #self.time.append(dt.time())
    self.open_.append(float(open_))
    self.high.append(float(high))
    self.low.append(float(low))
    self.close.append(float(close))
    self.volume.append(int(volume))

#   def to_csv(self):
#     return ''.join(["{0},{1},{2},{3:.2f},{4:.2f},{5:.2f},{6:.2f},{7}\n".format(self.symbol,
#               self.date[bar].strftime('%Y-%m-%d'),self.time[bar].strftime('%H:%M:%S'),
#               self.open_[bar],self.high[bar],self.low[bar],self.close[bar],self.volume[bar])
#               for bar in xrange(len(self.close))])

  def to_csv(self):
    return ''.join(["{0},{1},{2:.3f},{3:.3f},{4:.3f},{5:.3f},{6:.3f},{7}\n".format(self.date[bar].strftime('%Y-%m-%d'),self.symbol,
              self.open_[bar],self.high[bar],self.low[bar],self.close[bar],self.close[bar],self.volume[bar])
              for bar in xrange(len(self.close))])

  def write_csv(self,filename):
    with open(filename,'w') as f:
      f.write(self.to_csv())

  def read_csv(self,filename):
    self.symbol = ''
    self.date,self.time,self.open_,self.high,self.low,self.close,self.volume = ([] for _ in range(6))
    for line in open(filename,'r'):
      symbol,ds,ts,open_,high,low,close,volume = line.rstrip().split(',')
      self.symbol = symbol
    #   dt = datetime.datetime.strptime(ds+' '+ts,self.DATE_FMT+' '+self.TIME_FMT)
      dt = datetime.datetime.strptime(ds+' '+ts,self.DATE_FMT)
      self.append(dt,open_,high,low,close,volume)
    return True

  def __repr__(self):
    return self.to_csv()

class GoogleIntradayQuote(Quote):
  ''' Intraday quotes from Google. Specify interval seconds and number of days '''
  def __init__(self,symbol,interval_seconds=86400,num_days=5):
    super(GoogleIntradayQuote,self).__init__()
    self.symbol = symbol.upper()
    url_string = "http://finance.google.com/finance/getprices?q={0}".format(self.symbol)
    url_string += "&x=KLSE&i={0}&p={1}d&f=d,o,h,l,c,v".format(interval_seconds,num_days)
    csv = urllib.urlopen(url_string).readlines()
    for bar in xrange(7,len(csv)):
      if csv[bar].count(',')!=5: continue
      offset,close,high,low,open_,volume = csv[bar].split(',')
      if offset[0]=='a':
        day = float(offset[1:])
        offset = 0
      else:
        offset = float(offset)
      open_,high,low,close = [float(x) for x in [open_,high,low,close]]
      dt = datetime.datetime.fromtimestamp(day+(interval_seconds*offset))
      self.append(dt,open_,high,low,close,volume)

name1 ='LCTITAN'
#if __name__ == '__main__':
q = GoogleIntradayQuote(name1,86400,30)
q.write_csv('name1.csv')

输出如下

2018-01-23,LCTITAN,5.150,5.210,5.100,5.180,5.180,731300
2018-01-24,LCTITAN,5.170,5.190,5.120,5.120,5.120,640400
2018-01-25,LCTITAN,5.120,5.200,5.110,5.130,5.130,1144400
2018-01-26,LCTITAN,5.140,5.150,5.110,5.110,5.110,282900
2018-01-29,LCTITAN,5.120,5.140,5.080,5.080,5.080,512300
2018-01-30,LCTITAN,5.080,5.570,5.080,5.360,5.360,7857300
2018-02-02,LCTITAN,5.390,5.520,5.390,5.500,5.500,4537900

我理想的输出是在保存的csv文件的第一行添加Date,Stock,Open,High,Low Close,Adj_Close,Volume

Date,Stock,Open,High,Low, Close,Adj_Close,Volume
2018-01-23,LCTITAN,5.150,5.210,5.100,5.180,5.180,731300
2018-01-24,LCTITAN,5.170,5.190,5.120,5.120,5.120,640400
2018-01-25,LCTITAN,5.120,5.200,5.110,5.130,5.130,1144400
2018-01-26,LCTITAN,5.140,5.150,5.110,5.110,5.110,282900
2018-01-29,LCTITAN,5.120,5.140,5.080,5.080,5.080,512300
2018-01-30,LCTITAN,5.080,5.570,5.080,5.360,5.360,7857300
2018-02-02,LCTITAN,5.390,5.520,5.390,5.500,5.500,4537900

输出q不是列表或字符串。我不知道如何在第一行添加列,因为我无法将其更改为pandas数据帧。

只要csv包含上面第一行列并且pd.read_csv('name1.csv')能够执行

1 个答案:

答案 0 :(得分:0)

假设write_csv正在编写csv输出,这样的事情可能会起作用:

import csv

headers = ['Date','Stock','Open','High','Low','Close','Adj_Close','Volume']

def write_csv(self,filename):
    with open(filename,'w') as f:
        out = csv.writer(f, delimiter='\t')
        out.writerow(headers)
        out.write(self.to_csv())