How to split a txt file into two separate txt when there is a blank line separating?










0















How can I split a single .txt file into two or more .txt files when a white line occours?



Here is a example of what my txt looks like:



a s d d d d s d f
f d e s s a d f s
a s d d d d s d f
f d e s s a d f s

dsdesd
dseesdse


I would like to know how to split this single text file into:



First txt file:



a s d d d d s d f
f d e s s a d f s
a s d d d d s d f
f d e s s a d f s


Second txt file:



dsdesd
dseesdse









share|improve this question



















  • 4





    Try writing some code, and if you get stuck, show the code to us.

    – John Zwinck
    Nov 14 '18 at 11:17











  • There is no python in this question.

    – Peter Wood
    Nov 14 '18 at 11:18











  • Possible duplicate of split-file-after-x-lines-at-blank-line.

    – Mayank Porwal
    Nov 14 '18 at 11:18











  • Split it on the double newline: contents.split('nn')

    – Peter Wood
    Nov 14 '18 at 11:20















0















How can I split a single .txt file into two or more .txt files when a white line occours?



Here is a example of what my txt looks like:



a s d d d d s d f
f d e s s a d f s
a s d d d d s d f
f d e s s a d f s

dsdesd
dseesdse


I would like to know how to split this single text file into:



First txt file:



a s d d d d s d f
f d e s s a d f s
a s d d d d s d f
f d e s s a d f s


Second txt file:



dsdesd
dseesdse









share|improve this question



















  • 4





    Try writing some code, and if you get stuck, show the code to us.

    – John Zwinck
    Nov 14 '18 at 11:17











  • There is no python in this question.

    – Peter Wood
    Nov 14 '18 at 11:18











  • Possible duplicate of split-file-after-x-lines-at-blank-line.

    – Mayank Porwal
    Nov 14 '18 at 11:18











  • Split it on the double newline: contents.split('nn')

    – Peter Wood
    Nov 14 '18 at 11:20













0












0








0








How can I split a single .txt file into two or more .txt files when a white line occours?



Here is a example of what my txt looks like:



a s d d d d s d f
f d e s s a d f s
a s d d d d s d f
f d e s s a d f s

dsdesd
dseesdse


I would like to know how to split this single text file into:



First txt file:



a s d d d d s d f
f d e s s a d f s
a s d d d d s d f
f d e s s a d f s


Second txt file:



dsdesd
dseesdse









share|improve this question
















How can I split a single .txt file into two or more .txt files when a white line occours?



Here is a example of what my txt looks like:



a s d d d d s d f
f d e s s a d f s
a s d d d d s d f
f d e s s a d f s

dsdesd
dseesdse


I would like to know how to split this single text file into:



First txt file:



a s d d d d s d f
f d e s s a d f s
a s d d d d s d f
f d e s s a d f s


Second txt file:



dsdesd
dseesdse






python python-3.x file text-editor






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 11:25









Peter Wood

16.4k33270




16.4k33270










asked Nov 14 '18 at 11:16









LRA98LRA98

84




84







  • 4





    Try writing some code, and if you get stuck, show the code to us.

    – John Zwinck
    Nov 14 '18 at 11:17











  • There is no python in this question.

    – Peter Wood
    Nov 14 '18 at 11:18











  • Possible duplicate of split-file-after-x-lines-at-blank-line.

    – Mayank Porwal
    Nov 14 '18 at 11:18











  • Split it on the double newline: contents.split('nn')

    – Peter Wood
    Nov 14 '18 at 11:20












  • 4





    Try writing some code, and if you get stuck, show the code to us.

    – John Zwinck
    Nov 14 '18 at 11:17











  • There is no python in this question.

    – Peter Wood
    Nov 14 '18 at 11:18











  • Possible duplicate of split-file-after-x-lines-at-blank-line.

    – Mayank Porwal
    Nov 14 '18 at 11:18











  • Split it on the double newline: contents.split('nn')

    – Peter Wood
    Nov 14 '18 at 11:20







4




4





Try writing some code, and if you get stuck, show the code to us.

– John Zwinck
Nov 14 '18 at 11:17





Try writing some code, and if you get stuck, show the code to us.

– John Zwinck
Nov 14 '18 at 11:17













There is no python in this question.

– Peter Wood
Nov 14 '18 at 11:18





There is no python in this question.

– Peter Wood
Nov 14 '18 at 11:18













Possible duplicate of split-file-after-x-lines-at-blank-line.

– Mayank Porwal
Nov 14 '18 at 11:18





Possible duplicate of split-file-after-x-lines-at-blank-line.

– Mayank Porwal
Nov 14 '18 at 11:18













Split it on the double newline: contents.split('nn')

– Peter Wood
Nov 14 '18 at 11:20





Split it on the double newline: contents.split('nn')

– Peter Wood
Nov 14 '18 at 11:20












1 Answer
1






active

oldest

votes


















2














If you know the file will only have one blank line, you can split the contents at the double newline character:



with open('input.txt') as f:
contents = f.read()

output1, output2 = contents.split('nn')

with open('output1.txt', 'w') as o1:
o1.write(output1)

with open('output2.txt', 'w') as o2:
o2.write(output2)


If your file has more than one blank line this will fail as the split will return more than 2 parts and try to assign them to only two names, output1 and output2. split can be told to only split a maximum amount of times so it may be safer to say:



output1, output2 = contents.split('nn', 1)


If there are two or more blank lines, output1 will be the contents up to the first blank line. output2 will be everything after the first blank line, including any further blank lines.



Of course, this can fail if there are no blank lines.






share|improve this answer

























  • thank you, this works perfectly

    – LRA98
    Nov 14 '18 at 11:36










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53298946%2fhow-to-split-a-txt-file-into-two-separate-txt-when-there-is-a-blank-line-separat%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














If you know the file will only have one blank line, you can split the contents at the double newline character:



with open('input.txt') as f:
contents = f.read()

output1, output2 = contents.split('nn')

with open('output1.txt', 'w') as o1:
o1.write(output1)

with open('output2.txt', 'w') as o2:
o2.write(output2)


If your file has more than one blank line this will fail as the split will return more than 2 parts and try to assign them to only two names, output1 and output2. split can be told to only split a maximum amount of times so it may be safer to say:



output1, output2 = contents.split('nn', 1)


If there are two or more blank lines, output1 will be the contents up to the first blank line. output2 will be everything after the first blank line, including any further blank lines.



Of course, this can fail if there are no blank lines.






share|improve this answer

























  • thank you, this works perfectly

    – LRA98
    Nov 14 '18 at 11:36















2














If you know the file will only have one blank line, you can split the contents at the double newline character:



with open('input.txt') as f:
contents = f.read()

output1, output2 = contents.split('nn')

with open('output1.txt', 'w') as o1:
o1.write(output1)

with open('output2.txt', 'w') as o2:
o2.write(output2)


If your file has more than one blank line this will fail as the split will return more than 2 parts and try to assign them to only two names, output1 and output2. split can be told to only split a maximum amount of times so it may be safer to say:



output1, output2 = contents.split('nn', 1)


If there are two or more blank lines, output1 will be the contents up to the first blank line. output2 will be everything after the first blank line, including any further blank lines.



Of course, this can fail if there are no blank lines.






share|improve this answer

























  • thank you, this works perfectly

    – LRA98
    Nov 14 '18 at 11:36













2












2








2







If you know the file will only have one blank line, you can split the contents at the double newline character:



with open('input.txt') as f:
contents = f.read()

output1, output2 = contents.split('nn')

with open('output1.txt', 'w') as o1:
o1.write(output1)

with open('output2.txt', 'w') as o2:
o2.write(output2)


If your file has more than one blank line this will fail as the split will return more than 2 parts and try to assign them to only two names, output1 and output2. split can be told to only split a maximum amount of times so it may be safer to say:



output1, output2 = contents.split('nn', 1)


If there are two or more blank lines, output1 will be the contents up to the first blank line. output2 will be everything after the first blank line, including any further blank lines.



Of course, this can fail if there are no blank lines.






share|improve this answer















If you know the file will only have one blank line, you can split the contents at the double newline character:



with open('input.txt') as f:
contents = f.read()

output1, output2 = contents.split('nn')

with open('output1.txt', 'w') as o1:
o1.write(output1)

with open('output2.txt', 'w') as o2:
o2.write(output2)


If your file has more than one blank line this will fail as the split will return more than 2 parts and try to assign them to only two names, output1 and output2. split can be told to only split a maximum amount of times so it may be safer to say:



output1, output2 = contents.split('nn', 1)


If there are two or more blank lines, output1 will be the contents up to the first blank line. output2 will be everything after the first blank line, including any further blank lines.



Of course, this can fail if there are no blank lines.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 14 '18 at 11:29

























answered Nov 14 '18 at 11:22









Peter WoodPeter Wood

16.4k33270




16.4k33270












  • thank you, this works perfectly

    – LRA98
    Nov 14 '18 at 11:36

















  • thank you, this works perfectly

    – LRA98
    Nov 14 '18 at 11:36
















thank you, this works perfectly

– LRA98
Nov 14 '18 at 11:36





thank you, this works perfectly

– LRA98
Nov 14 '18 at 11:36



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53298946%2fhow-to-split-a-txt-file-into-two-separate-txt-when-there-is-a-blank-line-separat%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Barbados

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3