jeudi 26 janvier 2017

Looping through files to search for keywords is not working

I have the following loop that loads in a .txt file, converts each line of text into a list item and then searches for three key words. It then should open the next file in the list and do the same. It keeps on opening the wrong file (I assume) and stores the wrong data for each file it reads.

for file in URL_File_Name_List_Text:
    with open(file,'r') as readfile:
        file_line = readfile.read().splitlines()
        for line in file_line:
            if "FILED AS OF DATE" in line:
                Filed_Date = line.replace("FILED AS OF DATE:", "").strip()
                Filed_Date_List.append(Filed_Date)
            elif "COMPANY CONFORMED NAME" in line:
                Company_Name = line.replace("COMPANY CONFORMED NAME:", "").strip()
                Company_Name_List.append(Company_Name)
            elif "CENTRAL INDEX KEY:" in line:
                central_index_key = line.replace("CENTRAL INDEX KEY:","")
                central_index_key = central_index_key.strip()
                central_index_key = central_index_key.lstrip('0')
                CIK_List_new.append(central_index_key)
            else:
                continue

Aucun commentaire:

Enregistrer un commentaire