removeLineBreaks.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. # removeLineBreaks.py
  2. #
  3. # Demonstration of the pyparsing module, converting text files
  4. # with hard line-breaks to text files with line breaks only
  5. # between paragraphs. (Helps when converting downloads from Project
  6. # Gutenberg - https://www.gutenberg.org/ - to import to word processing apps
  7. # that can reformat paragraphs once hard line-breaks are removed.)
  8. #
  9. # Uses parse actions and transformString to remove unwanted line breaks,
  10. # and to double up line breaks between paragraphs.
  11. #
  12. # Copyright 2006, by Paul McGuire
  13. #
  14. import pyparsing as pp
  15. line_end = pp.LineEnd()
  16. # define an expression for the body of a line of text - use a predicate condition to
  17. # accept only lines with some content.
  18. def mustBeNonBlank(t):
  19. return t[0] != ''
  20. # could also be written as
  21. # return bool(t[0])
  22. lineBody = pp.SkipTo(line_end).addCondition(mustBeNonBlank, message="line body can't be empty")
  23. # now define a line with a trailing lineEnd, to be replaced with a space character
  24. textLine = lineBody + line_end().setParseAction(pp.replaceWith(" "))
  25. # define a paragraph, with a separating lineEnd, to be replaced with a double newline
  26. para = pp.OneOrMore(textLine) + line_end().setParseAction(pp.replaceWith("\n\n"))
  27. # run a test
  28. test = """
  29. Now is the
  30. time for
  31. all
  32. good men
  33. to come to
  34. the aid of their
  35. country.
  36. """
  37. print(para.transformString(test))
  38. # process an entire file
  39. # Project Gutenberg EBook of Successful Methods of Public Speaking, by Grenville Kleiser
  40. # Download from http://www.gutenberg.org/cache/epub/18095/pg18095.txt
  41. #
  42. with open("18095-8.txt") as source_file:
  43. original = source_file.read()
  44. # use transformString to convert line breaks
  45. transformed = para.transformString(original)
  46. with open("18095-8_reformatted.txt", "w") as transformed_file:
  47. transformed_file.write(transformed)