Last active
February 5, 2020 11:46
-
-
Save jayzhan211/b8604269842f3b62c1ba10fe8367a750 to your computer and use it in GitHub Desktop.
file_reformat
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
file format | |
* abc | |
* abcd | |
* abcde | |
* pef | |
* zxc | |
* zxcv | |
""" | |
with open('song.txt', 'r', encoding='utf-8-sig') as f: | |
content = f.readlines() | |
author = {} | |
song = [] | |
name = None | |
for s in content: | |
if s[0] == '\n': continue | |
if s[0] != '*': | |
author[name].append(s) | |
else: | |
name = s | |
author[name] = list() | |
author = sorted(author.items(), key=lambda kv: kv[0]) | |
with open('reformat_title.txt', 'w', encoding='utf-8-sig') as f: | |
for name, song_list in author: | |
song_list = sorted(song_list) | |
f.write(name) | |
for song in sorted(song_list): | |
f.write(song) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment