Help me with this function: def readdirwithmetadata(…
- User
Help me with this function:
def read_dir_with_metadata( dir="/XXXivritai_transcripts/Interviews_20250901/Interviews", out_name="ivritai_raw_data_20250910"): """ read a directory with srt / txt files load the metadata xlsx file mapping the filename to all the other metadata Mutual column: transcription / filename :return: """ data = read_dir_to_df(dir, out_name=out_name, merge_text=False, ftype=".txt") metadata = pd.read_excel(f"{dir}/Edut710.Transcription List 20250820.xlsx") metadata.rename(columns={"transcriptions": "filename"}, inplace=True) merged = pd.merge(data, metadata, on="filename", how="left")
# find any rows with missing "Identifier" values missing_ids = merged[merged["Identifier"].isna()] if missing_ids.shape[0] > 0: # replace the char "'" in the filename column with "" missing_ids["filename"] = missing_ids["filename"].str.replace("'", "") # merge the missing ids with the already merged dataframe pass - Assistant
Got it — so right now your function loads text files, reads metadata from Excel, merges by filename, and then tries to handle cases where Identifier is missing. The pass is where you want to "fix" mismatches caused by ' in the filenames. Here’s a clean way to do it: pythonCopy codedef read_dir_with_metadata( dir="/XXXivritai_transcripts/Interviews_20250901/Interviews", out_name="ivritai_raw_data_20250910"): """ Read a directory with srt/txt files and merge with metadata. Mutual column: transcription / filename :return: pd.DataFrame """ # read raw data data = read_dir_to_df( dir, out_name=out_name, merge_text=False, ftype=".txt" )