Advertisement
Guest User

songcloze

a guest
Jan 19th, 2018
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.80 KB | None | 0 0
  1. #!/usr/bin/env python
  2. from sys import argv
  3.  
  4. # to clarify a few messy spots
  5. def next_line(file):
  6.     return (file.readline().rstrip())
  7.  
  8. def generate_tag(title):
  9.     # Pattern for changing song titles to tags. If you prefer underscores
  10.     # to CamelCase, you can use title.replace(' ', '_') instead here.
  11.     return title.replace(' ', '')
  12.  
  13. # Ask user for file and song names.
  14. input_file = raw_input("Lyrics (Input) File:")
  15. output_file = raw_input("Cards (Output) File (Return for inputfile.tsv): ")
  16. song_title = raw_input("Song Title: ")
  17.  
  18. # Remove spaces to generate tags.
  19. tag = generate_tag(song_title)
  20.  
  21. # Open input and output files.
  22. lyrics_file = open(input_file)
  23. anki_file = open(output_file, 'w')
  24.  
  25. # Find total lines in file for the loop.
  26. total_lines = len(lyrics_file.readlines())
  27. lyrics_file.seek(0)
  28.  
  29. # Data string to be added to.
  30. cards_data = ""
  31.  
  32. # Make the first lines, as they're different from the rest (there aren't two
  33. # lines of context to use yet).
  34. lyrics_first_line = next_line(lyrics_file)
  35. lyrics_second_line = next_line(lyrics_file)
  36. lyrics_file.seek(0)
  37.  
  38. line1 = "[First Line] (%s)\t%s\n" % (song_title, lyrics_first_line)
  39. line2 = "[Beginning]<br>%s\t%s\n" % (lyrics_first_line, lyrics_second_line)
  40. cards_data += line1
  41. cards_data += line2
  42.  
  43. # Set variables to be used in the loop.
  44. context1 = next_line(lyrics_file)
  45. context2 = next_line(lyrics_file)
  46. current_line = next_line(lyrics_file)
  47.  
  48. # Loop through the remainder of the cards.
  49. i = 3 # We already did the first two cards
  50. while i <= total_lines:
  51.     cards_data += "%s<br>%s\t%s\n" % (context1, context2, current_line)
  52.     context1 = context2
  53.     context2 = current_line
  54.     current_line = next_line(lyrics_file)
  55.     i += 1
  56.  
  57. # Write cards and clean up.
  58. anki_file.write(cards_data)
  59. anki_file.close()
  60. lyrics_file.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement