# coding=utf-8
import os
import csv
import datetime
import re


home = os.path.expanduser('~')
mainfolder = home + '/scrapecollect/'
nysefile = mainfolder + 'healthcare-NYSE_filings_sorted_final.csv'
nyselinks = mainfolder + 'healthcare-NYSE_links.csv'
nyseview = mainfolder + 'healthcare-NYSE-view.csv'
nysefinal = mainfolder + 'healthcare-NYSE-view_final.csv'


with open(nysefile, 'rt') as testfile, open(nyselinks, 'rt') as testfile2:
    reader = csv.reader(testfile)
    reader2 = csv.reader(testfile2)
    writer = csv.writer(open(nyseview, 'wt'), delimiter = ',', quoting = csv.QUOTE_NONE, escapechar = ' ')
    testlist = list(reader)
    testlist2 = list(reader2)

    del testlist[0]
    del testlist2[0]
    badtext = 'Interactive'
    for i in testlist:
        if badtext in i[3]:
            i[3] = 'Documents'

    for a in testlist:
        tick = str(a[1]).strip()
        print(tick)
        for row in testlist2:
            tick1 = row[0]
            print(tick1)
            link = row[1]
            a[3] = '<a href=\"' + link + '\">' + 'Documents' + '</a>'
            if tick1 == tick:
                writer.writerow(a)

    testfile.close()
    testfile2.close()


with open(nyseview) as testfile3, open(nysefinal, 'wt') as testfile4:
    reader = csv.reader(testfile3)
    writer = csv.writer(testfile4)
    finalrows = []
    for x in testfile3:
        

    writer.writerow(finalrows)
    print('finished')


rowz = open(nyseview).read().split('\n')
newrows = []
for row in rowz:
    if row not in newrows:
        newrows.append(row)
f = open(home + '/scrapecollect/sdfsdf.csv', 'w')
f.write('\n'.join(newrows))
f.close()

nospaces = re.sub('\s\s+', ' ', x)
        finalrows.append(str(nospaces))