X
X
XuliGan4eg20062020-08-14 09:58:38
Python
XuliGan4eg2006, 2020-08-14 09:58:38

How to write separate values ​​from an array to a json file?

I have 3 files:
Song1.mp3
Song2.mp3
Song3.mp3

And I wrote the following script to get song titles and write them to an array:

import glob, os

massiv = []

for file in glob.glob("*.mp3"):
  massiv.append(str(file))
print(massiv)

And it gives this result:
['Song1.mp3', 'Song2.mp3', 'Song3.mp3']

Question:
How can I write the names of songs from an array into a JSON file like this:
[{"title":" Here is the name of the song1","file":" https://site.com/Here is the name of the song1"},{"title":"Here is the name of the song2","file":" Here is the name songs2"} etc.

PS
There will be a lot of songs (not exactly 3)

Help please! I will be very grateful for the answer!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2020-08-14
@XuliGan4eg2006

>>> songs_titles = ['Песня1.mp3', 'Песня2.mp3', 'Песня3.mp3']
>>> songs = [{'title': s, 'file': f'https://site.com/{s}'} for s in songs_titles]
>>> songs
[{'title': 'Песня1.mp3', 'file': 'https://site.com/Песня1.mp3'}, {'title': 'Песня2.mp3', 'file': 'https://site.com/Песня2.mp3'}, {'title': 'Песня3.mp3', 'file': 'https://site.com/Песня3.mp3'}]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question