I have an audio (wav) file imported into Python using the pydub
package. I have a dictionary consisting of each key matching to 2 values. The key is meant to be used as the name for the variable in a subsequent step for variable creation. The values are to be used to designate as the cut-off points to cut the audio file (start and end timestamp).
I want to create a for loop that creates multiple variables with the name of the key and the corresponding value to be the sliced audio file, with the audio file variable being named "audio" (e.g. audio[0:300000])
Here is a sample of my dictionary:
pairs = {
"part1": [0, 300000],
"part2": [300001, 600000],
"part3": [600001, 900000],
"part4": [900001, 1200000]
}
I've written the following code, but I'm unsure how to dynamically create a variable with the actual sliced audio file in a for loop.
for key, start_end in pairs.items():
start, end = start_end
sliced_audio = audio[start:end]
Other SO posts mentioned the following, but I do not want strings. I want the actual variables with the sliced audio file in them. Thanks!
print(f"{key} = {sliced_audio}")
As in the comments, don't dynamically create variables. Instead create a new dictionary:
audio_parts = {}
for key, start_end in pairs.items():
start, end = start_end
sliced_audio = audio[start:end]
audio_parts[key] = sliced_audio