Skip to content

Instantly share code, notes, and snippets.

@benhg
Created March 11, 2019 03:11
Show Gist options
  • Save benhg/3cf31769062c54266a70e1b7f77d4b2a to your computer and use it in GitHub Desktop.
Save benhg/3cf31769062c54266a70e1b7f77d4b2a to your computer and use it in GitHub Desktop.
Formats text poems for poembot. Ensures that there are only 32 characters per line. Breaks up on whole words only.
strin = " ".join(open(input("Poem File?\n") + ".txt", "r").readlines())
def split_string(str, limit, sep=" "):
words = str.split()
if max(map(len, words)) > limit:
raise ValueError("limit is too small")
res, part, others = [], words[0], words[1:]
for word in others:
if len(sep) + len(word) > limit - len(part):
res.append(part)
part = word
else:
part += sep + word
if part:
res.append(part)
return res
broken = split_string(strin, limit=32)
new = input("what day is this for?")
with open(new+".txt", "w") as fh:
for line in broken:
print(len(line))
fh.write(line+"\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment