forked from dulearnaux/NetfondsData
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_lists.py
100 lines (79 loc) · 2.59 KB
/
get_lists.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# -*- coding: utf-8 -*-
import os
import re
def get_list_tickers_in_dir(directory=None):
"""
gets the list of tickers in the directory
"""
start_dir = os.getcwd()
if directory == None:
directory = start_dir
listdir=os.listdir(directory)
TCKRS=[]
for ls in listdir:
# if not ls.endswith('.csv'):
# continue
check = re.match("[A-Z]*?\.[A-Z].",ls)
if check==None:
continue
TCKRS.append(check.group()[:-1])#we do not wan to include the last '.' in TCKRS
#remove duplicates
TCKRS = list(set(TCKRS))
os.chdir(start_dir)
return (TCKRS, listdir)
def get_list_tar_tickers_in_dir(directory):
"""
get the list of tickers that have been archived to TAR files
"""
start_dir = os.getcwd()
if directory == None:
directory = start_dir
listdir=os.listdir(directory)
TCKRS=[]
for ls in listdir:
# if not ls.endswith('.csv'):
# continue
check = re.match("[A-Z]*?\.[A-Z].tar",ls)
if check==None:
continue
TCKRS.append(check.group())
#remove duplicates
TCKRS = list(set(TCKRS))
os.chdir(start_dir)
return TCKRS
def get_csv_file_list(TCKR,listdir, directory=None):
"""
Input: single ticker in format 'TICKER.X', where X is netfonds exchange letter (N:NYSE,O:NASDAQ,A:AMEX)
Returns the list of files with ticker=TCKR
"""
start_dir = os.getcwd() #save start dir so we can revert back at the end of program
if directory==None:
directory = start_dir
os.chdir(directory)
#enables not entering valid listdir
if listdir==0:
listdir = os.listdir(directory)
ls = str(listdir)
#search for single run files
check = re.search('\''+TCKR+'\.[0-9]*\.csv\', ',ls)
if check == None:
print 'Error: ticker='+TCKR+' single date csv not found in:'
print directory
return 'no tickers'
files=[]
while check !=None:
s = str(check.group())
s=s.replace("'","")
s=s.replace(", ","")
files.append(s)
ls=ls.replace(check.group(),'xxxxx, ',1) #replace 1st instance of filename wit xxxxx
check = re.search("\'"+TCKR+"\.[0-9]*\.csv\', ",ls)
#search for combined files
check = re.search('\''+TCKR+'\.combined.csv\', ',ls)
if check !=None:
s = str(check.group())
s=s.replace("'","")
s=s.replace(", ","")
files.append(s)
os.chdir(start_dir)
return files