在文本文件中输出/保存某个字符串

时间:2016-01-13 14:32:48

标签: python python-3.x text

让我们说我用Python3打开一个文本文件:

fname1 = "filename.txt"

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

输出是段落的标准文本文件:

\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?"

如何只保存此文件中的某个字符串?例如,如何保存句子

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.

进入单独的文本文件?你怎么知道索引在哪里访问这句话?

澄清:不应该做出任何决定声明。最终目标是创建一个用户可以“保存”的程序。句子或段落分开。我现在问一个更普遍的问题。

让我们说在本文中我喜欢某个段落。我想要一种方法将其快速附加到JSON文件或文本文件。原则上,如何做到这一点?标记所有句子?有没有办法隔离段落? (重复上文)你怎么知道索引在哪里访问这句话? (特别是如果没有"决策逻辑")

如果我知道指数,我不能简单地切割字符串吗?

0 个答案:

没有答案