Created
November 20, 2019 18:12
-
-
Save koverholt/36a7a427181b95a2e2ff1191f04d7617 to your computer and use it in GitHub Desktop.
Split delimited Markdown files into smaller files
This file contains hidden or 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
| with open("export.md", "r") as f: | |
| buff = [] | |
| i = 1 | |
| for line in f: | |
| if line.strip() and line.strip() != "---": | |
| buff.append(line) | |
| # Preserve newlines in file | |
| if not line.strip(): | |
| buff.append("\n") | |
| # Write out smaller files | |
| if line.strip() == "---": | |
| # Strip newlines at beginning of file | |
| while buff[0] == "\n": | |
| buff.pop(0) | |
| # Strip newlines at end of file | |
| while buff[-1] == "\n": | |
| buff.pop(-1) | |
| output = open("import-" + "%d.txt" % i, "w") | |
| output.write(''.join(buff)) | |
| output.close() | |
| i += 1 | |
| buff = [] | |
| # Write out final small file | |
| # Strip newlines at beginning of file | |
| while buff[0] == "\n": | |
| buff.pop(0) | |
| # Strip newlines at end of file | |
| while buff[-1] == "\n": | |
| buff.pop(-1) | |
| output = open("import-" + "%d.txt" % i, "w") | |
| output.write(''.join(buff)) | |
| output.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment