python-金融数据处理demo

1.

扫描当前目录下过滤的文件比如所有csv文件名

遍历所有csv文件进行读取数据处理

数据处理结果写入result.csv文件里面输出 

(python2.78)

import glob,os,pdb,csv
count=0
amount=0
for filename in glob.glob('data/*.csv'):  
    reader = csv.reader(file(filename,'rb'))
    for line in reader:   
        if reader.line_num == 1:  
            continue
        #print line[3]
        #pdb.set_trace()
        count+=1
        amount+=(float)(line[3])
writer = csv.writer(open("result.csv","wb"),quoting=csv.QUOTE_ALL)  
writer.writerow([count,amount])
#glob.glob('E:/data/*.csv')

python pdb调试技巧参考:http://www.ibm.com/developerworks/cn/linux/l-cn-pythondebugger/

2.其他遍历目录获取当前文件名的方法

import os
filenames=os.listdir(os.getcwd())
for name in filenames:
     filenames[filenames.index(name)]=name[:-3]
print filenames
raw_input();

3.

filename = r'a.txt'
alist = []
lines = open(filename,'r').readlines()
alist = [line.strip().split(',')[3] for line in lines ]#字段以逗号分隔,这里取得是第4列

filename = r'a.txt'
alist = []
lines = open(filename,'r').readlines()
alist = [line.strip().split()[1] for line in lines ]#字段以空格分隔(如果是tab,请换为\t),这里取得是第2列

4.时间

yesterday=float((datetime.date.today()-datetime.timedelta(days=1)).strftime('%Y%m%d'))




你可能感兴趣的:(python-金融数据处理demo)