import os import re def extract(): result = {} working_dir = os.path.dirname(os.path.realpath(__file__)) rpp_filename = os.path.join(working_dir, '..', r'project.rpp') with open(rpp_filename, mode='r', encoding='utf-8') as rpp_file: rpp_content = rpp_file.readlines() for rpp_content_line in rpp_content: pattern = r'^.*.*$' if re.match(pattern, rpp_content_line): is_playlist = False if is_playlist: playlist_item = rpp_content_line.strip() playlist_item = playlist_item[:-2] playlist_item = playlist_item[8:] playlist_item = int(playlist_item) - 24 for rpp_content_line2 in rpp_content: pattern = r'^.*MARKER ' + str(playlist_item) + ' .*".+".*{.*}.*$' # Ouch if re.match(pattern, rpp_content_line2): item = rpp_content_line2.strip() item = item.split('"')[1] result[playlist_name].append(item) pattern = r'^.*' + playlist_name + '.*$' if re.match(pattern, rpp_content_line): is_playlist = True save2csv(result) def save2csv(result): for playlist in result: working_dir = os.path.dirname(os.path.realpath(__file__)) playlist_file_name = os.path.join(working_dir, playlist + '.csv') with open(playlist_file_name, mode='w', encoding="iso-8859-1") as playlist_file: for item in result[playlist]: playlist_file.write(item + '\n') if __name__ == '__main__': extract()