Skip to content

Instantly share code, notes, and snippets.

@jayzhan211
Last active February 5, 2020 11:46
Show Gist options
  • Save jayzhan211/b8604269842f3b62c1ba10fe8367a750 to your computer and use it in GitHub Desktop.
Save jayzhan211/b8604269842f3b62c1ba10fe8367a750 to your computer and use it in GitHub Desktop.
file_reformat
"""
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