0

Let's say I open a text file with Python3:

fname1 = "filename.txt"

with open(fname1, "rt", encoding='latin1') as in_file:
    readable_file = in_file.read()

The output is a standard text file of paragraphs:

\n\n"Well done, Mrs. Martin!" thought Emma.  "You know what you are about."\n\n"And when she had come away, Mrs. Martin was so very kind as to send\nMrs. Goddard a beautiful goose--the finest goose Mrs. Goddard had\never seen.  Mrs. Goddard had dressed it on a Sunday, and asked all\nthe three teachers, Miss Nash, and Miss Prince, and Miss Richardson,\nto sup with her."\n\n"Mr. Martin, I suppose, is not a man of information beyond the line\nof his own business? He does not read?"\n\n"Oh yes!--that is, no--I do not know--but I believe he has\nread a good deal--but not what you would think any thing of.\nHe reads the Agricultural Reports, and some other books that lay\nin one of the window seats--but he reads all _them_ to himself.\nBut sometimes of an evening, before we went to cards, he would read\nsomething aloud out of the Elegant Extracts, very entertaining.\nAnd I know he has read the Vicar of Wakefield.  He never read the\nRomance of the Forest, nor The Children of the Abbey.  He had never\nheard of such books before I mentioned them, but he is determined\nto get them now as soon as ever he can."\n\nThe next question was--\n\n"What sort of looking man is Mr. Martin?"

How can one save only a certain string within this file? For example, how does one save the sentence

And when she had come away, Mrs. Martin was so very kind as to send\nMrs. Goddard a beautiful goose--the finest goose Mrs. Goddard had\never seen.

into a separate text file? How do you know the indices where to access this sentence?

CLARIFICATION: There should be no decision statements to make. The end goal is to create a program which the user could "save" sentences or paragraphs separately. I am asking a more general question at the moment.

Let's say there's a certain paragraph I like in this text. I would like a way to append this quickly to a JSON file or text file. In principle, how does one do this? Tagging all sentences? Is there a way to isolate paragraphs? (To repeat above) How do you know the indices where to access this sentence? (especially if there is no "decision logic")

If I know the indices, couldn't I simply slice the string?

EB2127
  • 1,788
  • 3
  • 22
  • 43
  • What piece of your input file do you want to write into another text file? You probably should do some string manipulation in order to get the extact string which you want. – giograno Jan 13 '16 at 14:40
  • Are you looking for a way to split the text into sentences? – MKesper Jan 13 '16 at 14:45
  • 1
    You can use [NLTK](http://stackoverflow.com/questions/4576077/python-split-text-on-sentences) for splitting a text in sentences. That is way better than reinventing the wheel again, especially since this wheel is very hard to create... – Nander Speerstra Jan 13 '16 at 14:48

0 Answers0