Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import json
- def export_to_json_from_files():
- directory = os.getcwd()
- json_data = {}
- # Check if the JSON file already exists
- if os.path.isfile("album_data.json"):
- with open("album_data.json", "r") as json_file:
- json_data = json.load(json_file)
- # Get the highest index in the existing JSON data
- highest_index = max(map(int, json_data.keys())) if json_data else 0
- for filename in os.listdir(directory):
- if filename.endswith(".txt"):
- filepath = os.path.join(directory, filename)
- with open(filepath, "r") as file:
- lines = file.readlines()
- scores = []
- for line in lines:
- track_info = line.split(":")
- if len(track_info) >= 2:
- score = track_info[1].strip()
- if score == "NR":
- scores.append("NR")
- else:
- try:
- scores.append(float(score))
- except ValueError:
- scores.append("Invalid Score")
- else:
- scores.append("Missing Score")
- average_score = float(lines[-1].split(":")[1].strip())
- artist, album = os.path.splitext(filename)[0].split(" - ")
- data = {
- "Artist_Title": artist,
- "Album_Title": album,
- "Scores": scores,
- "Average_Score": average_score
- }
- # Assign the next available index
- highest_index += 1
- json_data[str(highest_index)] = data
- with open("album_data.json", "w") as json_file:
- json.dump(json_data, json_file, indent=4)
- export_to_json_from_files()
Advertisement
Add Comment
Please, Sign In to add comment