python 金融資料處理demo

2021-06-26 18:19:04 字數 1409 閱讀 7556

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除錯技巧參考:

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金融資料處理之Pandas包

在python的pandas包中,有兩種資料結構可以很方便地用於儲存複雜的資料,為series和dataframe。一 series 首先先講一下series,series是dataframe的基礎。series可以認為是個具有索引 index 的一維陣列,可以和程式設計中另乙個常用的概念hash ...

python 呼叫gdal 處理dem資料

coding utf 8 from osgeo import gdal import osr import numpy as np from matplotlib import pyplot as plt from gdalconst import from matplotlib import cm...

Python 資料處理

將檔案切分,存入列表 strip split with open james.txt as jaf data jaf.readline james data.strip split 資料檔案為 2 34,3 21,2.34,2.45,3.01,2 01,2 01,3 10,2 22 print ja...