-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompany_filing_to_cvs.py
More file actions
82 lines (57 loc) · 2.64 KB
/
Company_filing_to_cvs.py
File metadata and controls
82 lines (57 loc) · 2.64 KB
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
import xml.etree.ElementTree as ET
import csv
import os
import re
filings_folder = 'C:\\Users\\willi\\Documents\\Company_filings\\BRK-A\\13F-HR\\sec_edgar_filings\\1067983\\13F-HR\\'
csv_folder = 'C:\\Users\\willi\\Documents\\Company_filings\\BRK-A\\13F-HR\\'
# NB todo:
# Rewrite script so that it operates more like the 'company_listed.py' script, searching for relevant 'start/stop' and extracting the relevant data (company_name and shares held + value of shares?), regardless of xml or txt format
def check_filing_type(check_file):
for row in check_file:
if "<TABLE>" in row:
return True
break
elif "<XML>" in row:
return False
break
else:
continue
for filing_name in os.listdir(filings_folder):
with open(filings_folder + filing_name, mode='r') as in_file:
check_filing_type(in_file)
start = False
content = []
if check_filing_type(in_file):
print(filing_name, " is text")
for line in in_file:
print(re.sub(' +', ';', line))
# filing is text-type
# extract information from filing array, or write directly to new document
else:
print(filing_name, " is xml")
# filing is xml-type
# extract information from filing to array, or write directly to new document
# open new write file
# for loop writing from array, or just writing filtered information directly
################################################################################################
with open(filings_folder + filing_name, mode='r') as in_file:
start_txt = False
start_xml = False
end = ''
for line in in_file:
if "Column 1" in line:
start_txt = True
with open(csv_folder + '-' + filing_name.replace('txt', 'cvs'), mode='w') as out_file:
stripped = (line.strip() for line in in_file if start_txt)
lines = (line.split(',') for line in in_file if start_txt)
writer = csv.writer(out_file)
writer.writerows(lines)
print(in_file, lines, out_file)
elif "<XML>" in line:
start_xml = True
break
with open(csv_folder + '-' + filing_name.replace('txt', 'xml'), mode='w') as out_file:
for copy_line in in_file:
out_file.write(copy_line)
print(in_file, type(out_file))
print('Done') # Prints progress