i am looking for an efficient way to read and append texts of .txt files to a dataframe. I currently have 10 folders with 100k documents each. What i specifically need to do is:
- getting the names of the files inside one folder (filenames contain important information like a unique ID for each company that issue the document and the date when the document was published, like this: "CIK_000000_DATE_14_12_2022.txt")
- extract from the name of the files the unique ID (CIK) and the date
- take the text related to the file
append this 3 pieces of informations in a dataset so that the dataset appear like:
CIK | date | text | serial |
---|---|---|---|
000000 | 11/12/2012 | some text here... | 1 |
000001 | 14/11/2019 | some other text here... | 2 |
the folders are individually made like this:
TXT_01
|_ CIK__000000__DATE__14-12-2012__serial__0.txt
|_ CIK__000001__DATE__12-11-2019__serial__1.txt
|_ CIK__000001__DATE__11-12-2014__serial__2.txt
|_ CIK__000175__DATE__11-04-2011__serial__3.txt
|_ ...
TXT_02
|_ CIK__000135__DATE__11-04-2001__serial__100.txt
|_ CIK__000115__DATE__11-04-2001__serial__101.txt
|_ CIK__000145__DATE__11-04-2001__serial__103.txt
|_ CIK__000155__DATE__11-04-2001__serial__104.txt
|_ ...
...
They get the job done, but the time they take to finish a test folder with 4000 documents is way too much considering that i need to do this for 100k files folders.
i can provide more informations if needed. I'm open to any advice, even learning faster programming languages in order to get this done.
thank you all
here the code i'm currently executing (consider that it comes from a collab notebook)
folders = []
names = os.listdir()
for i in names:
if i.startswith('TXT'):
folders.append(i)
def get_file_names(folder):
return os.listdir(folder)
def get_cik_date(file):
cik = re.findall(r"CIK__([0-9]*)", file)[0].zfill(10)
date = re.findall(r"date__(([0-9]*)-([0-9]*)-([0-9]*))", file)[0][0]
serial = re.findall(r"serial__([0-9]*)", file)[0]
return (cik, date, serial)
def get_text(file):
get_text.counter +=1
print(get_text.counter)
with open(file) as f:
lines = f.readlines()
return lines
get_text.counter = 0
file_names = list(map(get_file_names,folders))
folder_names = list(zip(folders,file_names))
cik_date_folder = [[get_cik_date(i) for i in j] for k,j in folder_names]
texts = [[get_text(f"{k}/{i}") for i in j] for k,j in folder_names]
test = [dict(zip(cik_date_folder[i], texts[i])) for i,n in enumerate(folders)]
df = pd.DataFrame.from_dict(test)
df = df.ffill().bfill().head(1).T
df_reset = df.reset_index()
df_reset = df_reset.rename({"index": "cik_date", 0:"text"}, axis = 1)
df_reset[['cik','dates', 'serial']] = pd.DataFrame(df_reset['cik_date'].tolist(),index=df_reset.index)
df_complete = df_reset.drop("cik_date", axis = 1)
def concat_list(text_as_list):
return " ".join(text_as_list)
df_complete["text"] = df_complete["text"].apply(concat_list)
```