How to insert random spaces in txt file?










1















I have a file with lines of DNA in a file called 'DNASeq.txt'. I need a code to read each line and split each line at random places (inserting spaces) throughout the line. Each line needs to be split at different places.



EX: I have:
AAACCCHTHTHDAFHDSAFJANFAJDSNFADKFAFJ
And I need something like this:
AAA ADSF DFAFDDSAF ADF ADSF AFD AFAD



I have tried (!!!very new to python!!):



import random

for x in range(10):
print(random.randint(50,250))


but that prints me random numbers. Is there some way to get a random number generated as like a variable?










share|improve this question



















  • 2





    Do you have a fixed number of splits, or is that random too?

    – Daniel Mesejo
    Nov 13 '18 at 20:22















1















I have a file with lines of DNA in a file called 'DNASeq.txt'. I need a code to read each line and split each line at random places (inserting spaces) throughout the line. Each line needs to be split at different places.



EX: I have:
AAACCCHTHTHDAFHDSAFJANFAJDSNFADKFAFJ
And I need something like this:
AAA ADSF DFAFDDSAF ADF ADSF AFD AFAD



I have tried (!!!very new to python!!):



import random

for x in range(10):
print(random.randint(50,250))


but that prints me random numbers. Is there some way to get a random number generated as like a variable?










share|improve this question



















  • 2





    Do you have a fixed number of splits, or is that random too?

    – Daniel Mesejo
    Nov 13 '18 at 20:22













1












1








1








I have a file with lines of DNA in a file called 'DNASeq.txt'. I need a code to read each line and split each line at random places (inserting spaces) throughout the line. Each line needs to be split at different places.



EX: I have:
AAACCCHTHTHDAFHDSAFJANFAJDSNFADKFAFJ
And I need something like this:
AAA ADSF DFAFDDSAF ADF ADSF AFD AFAD



I have tried (!!!very new to python!!):



import random

for x in range(10):
print(random.randint(50,250))


but that prints me random numbers. Is there some way to get a random number generated as like a variable?










share|improve this question
















I have a file with lines of DNA in a file called 'DNASeq.txt'. I need a code to read each line and split each line at random places (inserting spaces) throughout the line. Each line needs to be split at different places.



EX: I have:
AAACCCHTHTHDAFHDSAFJANFAJDSNFADKFAFJ
And I need something like this:
AAA ADSF DFAFDDSAF ADF ADSF AFD AFAD



I have tried (!!!very new to python!!):



import random

for x in range(10):
print(random.randint(50,250))


but that prints me random numbers. Is there some way to get a random number generated as like a variable?







python python-3.x insert spaces






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 20:28









petezurich

3,57581734




3,57581734










asked Nov 13 '18 at 20:20









Alpa LucaAlpa Luca

85




85







  • 2





    Do you have a fixed number of splits, or is that random too?

    – Daniel Mesejo
    Nov 13 '18 at 20:22












  • 2





    Do you have a fixed number of splits, or is that random too?

    – Daniel Mesejo
    Nov 13 '18 at 20:22







2




2





Do you have a fixed number of splits, or is that random too?

– Daniel Mesejo
Nov 13 '18 at 20:22





Do you have a fixed number of splits, or is that random too?

– Daniel Mesejo
Nov 13 '18 at 20:22












4 Answers
4






active

oldest

votes


















0














You can read a file line wise, write each line character-wise in a new file and insert spaces randomly:



Create demo file without spaces:



with open("t.txt","w") as f:
f.write("""ASDFSFDGHJEQWRJIJG
ASDFJSDGFIJ
SADFJSDFJJDSFJIDFJGIJSRGJSDJFIDJFG
SDFJGIKDSFGOROHPTLPASDMKFGDOKRAMGO""")


Read and rewrite demo file:



import random
max_no_space = 9 # if max sequence length without space
no_space = 0
with open("t.txt","r") as f, open("n.txt","w") as w:
for line in f:
for c in line:
w.write(c)
if random.randint(1,6) == 1 or no_space >= max_no_space:
w.write(" ")
no_space = 0
else:
no_space += 1
with open("n.txt") as k:
print(k.read())


Output:



ASDF SFD GHJEQWRJIJG 
A SDFJ SDG FIJ
SADFJSD FJ JDSFJIDFJG I JSRGJSDJ FIDJFG


The pattern of spaces is random. You can influence it by settin max_no_spaces or remove the randomness to split after max_no_spaces all the time




Edit:



This way of writing 1 character at a time if you need to read 200+ en block is not very economic, you can do it with the same code like so:



with open("t.txt","w") as f:
f.write("""ASDFSFDGHJEQWRJIJSADFJSDFJJDSFJIDFJGIJSRGJSDJFIDJFGG
ASDFJSDGFIJSADFJSDFJJDSFJIDFJGIJSRGJSDJFIDJFGSADFJSDFJJDSFJIDFJGIJK
SADFJSDFJJDSFJIDFJGIJSRGJSDJFIDJFGSADFJSDFJJDSFJIDFJGIJSRGJSDJFIDJF
SDFJGIKDSFGOROHPTLPASDMKFGDOKRAMGSADFJSDFJJDSFJIDFJGIJSRGJSDJFIDJFG""")


import random
min_no_space = 10
max_no_space = 20 # if max sequence length without space
no_space = 0
with open("t.txt","r") as f, open("n.txt","w") as w:
for line in f:
for c in line:
w.write(c)
if no_space > min_no_space:
if random.randint(1,6) == 1 or no_space >= max_no_space:
w.write(" ")
no_space = 0
else:
no_space += 1
with open("n.txt") as k:
print(k.read())


Output:



ASDFSFDGHJEQ WRJIJSADFJSDF JJDSFJIDFJGIJ SRGJSDJFIDJFGG
ASDFJSDGFIJSA DFJSDFJJDSFJIDF JGIJSRGJSDJFIDJ FGSADFJSDFJJ DSFJIDFJGIJK
SADFJ SDFJJDSFJIDFJG IJSRGJSDJFIDJ FGSADFJSDFJJDS FJIDFJGIJSRG JSDJFIDJF
SDFJG IKDSFGOROHPTLPASDMKFGD OKRAMGSADFJSDF JJDSFJIDFJGI JSRGJSDJFIDJFG





share|improve this answer

























  • Thanks a million. I really appreciate it!

    – Alpa Luca
    Nov 14 '18 at 16:43











  • Is there a way to set the minimum sequence length without spaces? I am trying to get sequences that range from about 200-500 base pairs in length and the code is great I just need to set a minimum length before it inserts a space ( no single base pairs alone). Your help is so greatly appreciated! Thanks so much!!!

    – Alpa Luca
    Nov 14 '18 at 16:54











  • @AlpaLuca see edit

    – Patrick Artner
    Nov 14 '18 at 17:27











  • THANKS SO MUCH!!! LIFE SAVER!!! THANKS AGAIN!

    – Alpa Luca
    Nov 14 '18 at 19:35


















0














If you want to split your DNA fixed amount of times (10 in my example) here's what you could try:



import random

DNA = 'AAACCCHTHTHDAFHDSAFJANFAJDSNFADKFAFJ'
splitted_DNA = ''

for split_idx in sorted(random.sample(range(len(DNA)), 10)):
splitted_DNA += DNA[len(splitted_DNA)-splitted_DNA.count(' ') :split_idx] + ' '
splitted_DNA += DNA[split_idx:]

print(splitted_DNA) # -> AAACCCHT HTH D AF HD SA F JANFAJDSNFA DK FAFJ





share|improve this answer























  • Thanks so much for your time!! I appreciate it!

    – Alpa Luca
    Nov 14 '18 at 16:43


















0














import random

with open('source', 'r') as in_file:
with open('dest', 'w') as out_file:
for line in in_file:
newLine = ''.join(map(lambda x:x+' '*random.randint(0,1), line)).strip() + 'n'
out_file.write(newLine)


Since you mentioned being new, I'll try to explain



  • I'm writing the new sequences to another file for precaution. It's
    not safe to write to the file you are reading from.


  • The with constructor is so that you don't need to explicitly close
    the file you opened.


  • Files can be read line by line using for loop.


  • ''.join() converts a list to a string.


  • map() applies a function to every element of a list and returns the
    results as a new list.


  • lambda is how you define a function without naming it. lambda x:
    2*x
    doubles the number you feed it.


  • x + ' ' * 3 adds 3 spaces after x. random.randint(0, 1) returns
    either 1 or 0. So I'm randomly selecting if I'll add a space after
    each character or not. If the random.randint() returns 0, 0 spaces are added.






share|improve this answer




















  • 1





    thanks so much!!

    – Alpa Luca
    Nov 14 '18 at 16:43


















0














You can toss a coin after each character whether to add space there or not.
This function takes string as input and returns output with space inserted at random places.



def insert_random_spaces(str):
from random import randint
output_string = "".join([x+randint(0,1)*" " for x in str])
return output_string





share|improve this answer
























    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%2f53288893%2fhow-to-insert-random-spaces-in-txt-file%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    4 Answers
    4






    active

    oldest

    votes








    4 Answers
    4






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    You can read a file line wise, write each line character-wise in a new file and insert spaces randomly:



    Create demo file without spaces:



    with open("t.txt","w") as f:
    f.write("""ASDFSFDGHJEQWRJIJG
    ASDFJSDGFIJ
    SADFJSDFJJDSFJIDFJGIJSRGJSDJFIDJFG
    SDFJGIKDSFGOROHPTLPASDMKFGDOKRAMGO""")


    Read and rewrite demo file:



    import random
    max_no_space = 9 # if max sequence length without space
    no_space = 0
    with open("t.txt","r") as f, open("n.txt","w") as w:
    for line in f:
    for c in line:
    w.write(c)
    if random.randint(1,6) == 1 or no_space >= max_no_space:
    w.write(" ")
    no_space = 0
    else:
    no_space += 1
    with open("n.txt") as k:
    print(k.read())


    Output:



    ASDF SFD GHJEQWRJIJG 
    A SDFJ SDG FIJ
    SADFJSD FJ JDSFJIDFJG I JSRGJSDJ FIDJFG


    The pattern of spaces is random. You can influence it by settin max_no_spaces or remove the randomness to split after max_no_spaces all the time




    Edit:



    This way of writing 1 character at a time if you need to read 200+ en block is not very economic, you can do it with the same code like so:



    with open("t.txt","w") as f:
    f.write("""ASDFSFDGHJEQWRJIJSADFJSDFJJDSFJIDFJGIJSRGJSDJFIDJFGG
    ASDFJSDGFIJSADFJSDFJJDSFJIDFJGIJSRGJSDJFIDJFGSADFJSDFJJDSFJIDFJGIJK
    SADFJSDFJJDSFJIDFJGIJSRGJSDJFIDJFGSADFJSDFJJDSFJIDFJGIJSRGJSDJFIDJF
    SDFJGIKDSFGOROHPTLPASDMKFGDOKRAMGSADFJSDFJJDSFJIDFJGIJSRGJSDJFIDJFG""")


    import random
    min_no_space = 10
    max_no_space = 20 # if max sequence length without space
    no_space = 0
    with open("t.txt","r") as f, open("n.txt","w") as w:
    for line in f:
    for c in line:
    w.write(c)
    if no_space > min_no_space:
    if random.randint(1,6) == 1 or no_space >= max_no_space:
    w.write(" ")
    no_space = 0
    else:
    no_space += 1
    with open("n.txt") as k:
    print(k.read())


    Output:



    ASDFSFDGHJEQ WRJIJSADFJSDF JJDSFJIDFJGIJ SRGJSDJFIDJFGG
    ASDFJSDGFIJSA DFJSDFJJDSFJIDF JGIJSRGJSDJFIDJ FGSADFJSDFJJ DSFJIDFJGIJK
    SADFJ SDFJJDSFJIDFJG IJSRGJSDJFIDJ FGSADFJSDFJJDS FJIDFJGIJSRG JSDJFIDJF
    SDFJG IKDSFGOROHPTLPASDMKFGD OKRAMGSADFJSDF JJDSFJIDFJGI JSRGJSDJFIDJFG





    share|improve this answer

























    • Thanks a million. I really appreciate it!

      – Alpa Luca
      Nov 14 '18 at 16:43











    • Is there a way to set the minimum sequence length without spaces? I am trying to get sequences that range from about 200-500 base pairs in length and the code is great I just need to set a minimum length before it inserts a space ( no single base pairs alone). Your help is so greatly appreciated! Thanks so much!!!

      – Alpa Luca
      Nov 14 '18 at 16:54











    • @AlpaLuca see edit

      – Patrick Artner
      Nov 14 '18 at 17:27











    • THANKS SO MUCH!!! LIFE SAVER!!! THANKS AGAIN!

      – Alpa Luca
      Nov 14 '18 at 19:35















    0














    You can read a file line wise, write each line character-wise in a new file and insert spaces randomly:



    Create demo file without spaces:



    with open("t.txt","w") as f:
    f.write("""ASDFSFDGHJEQWRJIJG
    ASDFJSDGFIJ
    SADFJSDFJJDSFJIDFJGIJSRGJSDJFIDJFG
    SDFJGIKDSFGOROHPTLPASDMKFGDOKRAMGO""")


    Read and rewrite demo file:



    import random
    max_no_space = 9 # if max sequence length without space
    no_space = 0
    with open("t.txt","r") as f, open("n.txt","w") as w:
    for line in f:
    for c in line:
    w.write(c)
    if random.randint(1,6) == 1 or no_space >= max_no_space:
    w.write(" ")
    no_space = 0
    else:
    no_space += 1
    with open("n.txt") as k:
    print(k.read())


    Output:



    ASDF SFD GHJEQWRJIJG 
    A SDFJ SDG FIJ
    SADFJSD FJ JDSFJIDFJG I JSRGJSDJ FIDJFG


    The pattern of spaces is random. You can influence it by settin max_no_spaces or remove the randomness to split after max_no_spaces all the time




    Edit:



    This way of writing 1 character at a time if you need to read 200+ en block is not very economic, you can do it with the same code like so:



    with open("t.txt","w") as f:
    f.write("""ASDFSFDGHJEQWRJIJSADFJSDFJJDSFJIDFJGIJSRGJSDJFIDJFGG
    ASDFJSDGFIJSADFJSDFJJDSFJIDFJGIJSRGJSDJFIDJFGSADFJSDFJJDSFJIDFJGIJK
    SADFJSDFJJDSFJIDFJGIJSRGJSDJFIDJFGSADFJSDFJJDSFJIDFJGIJSRGJSDJFIDJF
    SDFJGIKDSFGOROHPTLPASDMKFGDOKRAMGSADFJSDFJJDSFJIDFJGIJSRGJSDJFIDJFG""")


    import random
    min_no_space = 10
    max_no_space = 20 # if max sequence length without space
    no_space = 0
    with open("t.txt","r") as f, open("n.txt","w") as w:
    for line in f:
    for c in line:
    w.write(c)
    if no_space > min_no_space:
    if random.randint(1,6) == 1 or no_space >= max_no_space:
    w.write(" ")
    no_space = 0
    else:
    no_space += 1
    with open("n.txt") as k:
    print(k.read())


    Output:



    ASDFSFDGHJEQ WRJIJSADFJSDF JJDSFJIDFJGIJ SRGJSDJFIDJFGG
    ASDFJSDGFIJSA DFJSDFJJDSFJIDF JGIJSRGJSDJFIDJ FGSADFJSDFJJ DSFJIDFJGIJK
    SADFJ SDFJJDSFJIDFJG IJSRGJSDJFIDJ FGSADFJSDFJJDS FJIDFJGIJSRG JSDJFIDJF
    SDFJG IKDSFGOROHPTLPASDMKFGD OKRAMGSADFJSDF JJDSFJIDFJGI JSRGJSDJFIDJFG





    share|improve this answer

























    • Thanks a million. I really appreciate it!

      – Alpa Luca
      Nov 14 '18 at 16:43











    • Is there a way to set the minimum sequence length without spaces? I am trying to get sequences that range from about 200-500 base pairs in length and the code is great I just need to set a minimum length before it inserts a space ( no single base pairs alone). Your help is so greatly appreciated! Thanks so much!!!

      – Alpa Luca
      Nov 14 '18 at 16:54











    • @AlpaLuca see edit

      – Patrick Artner
      Nov 14 '18 at 17:27











    • THANKS SO MUCH!!! LIFE SAVER!!! THANKS AGAIN!

      – Alpa Luca
      Nov 14 '18 at 19:35













    0












    0








    0







    You can read a file line wise, write each line character-wise in a new file and insert spaces randomly:



    Create demo file without spaces:



    with open("t.txt","w") as f:
    f.write("""ASDFSFDGHJEQWRJIJG
    ASDFJSDGFIJ
    SADFJSDFJJDSFJIDFJGIJSRGJSDJFIDJFG
    SDFJGIKDSFGOROHPTLPASDMKFGDOKRAMGO""")


    Read and rewrite demo file:



    import random
    max_no_space = 9 # if max sequence length without space
    no_space = 0
    with open("t.txt","r") as f, open("n.txt","w") as w:
    for line in f:
    for c in line:
    w.write(c)
    if random.randint(1,6) == 1 or no_space >= max_no_space:
    w.write(" ")
    no_space = 0
    else:
    no_space += 1
    with open("n.txt") as k:
    print(k.read())


    Output:



    ASDF SFD GHJEQWRJIJG 
    A SDFJ SDG FIJ
    SADFJSD FJ JDSFJIDFJG I JSRGJSDJ FIDJFG


    The pattern of spaces is random. You can influence it by settin max_no_spaces or remove the randomness to split after max_no_spaces all the time




    Edit:



    This way of writing 1 character at a time if you need to read 200+ en block is not very economic, you can do it with the same code like so:



    with open("t.txt","w") as f:
    f.write("""ASDFSFDGHJEQWRJIJSADFJSDFJJDSFJIDFJGIJSRGJSDJFIDJFGG
    ASDFJSDGFIJSADFJSDFJJDSFJIDFJGIJSRGJSDJFIDJFGSADFJSDFJJDSFJIDFJGIJK
    SADFJSDFJJDSFJIDFJGIJSRGJSDJFIDJFGSADFJSDFJJDSFJIDFJGIJSRGJSDJFIDJF
    SDFJGIKDSFGOROHPTLPASDMKFGDOKRAMGSADFJSDFJJDSFJIDFJGIJSRGJSDJFIDJFG""")


    import random
    min_no_space = 10
    max_no_space = 20 # if max sequence length without space
    no_space = 0
    with open("t.txt","r") as f, open("n.txt","w") as w:
    for line in f:
    for c in line:
    w.write(c)
    if no_space > min_no_space:
    if random.randint(1,6) == 1 or no_space >= max_no_space:
    w.write(" ")
    no_space = 0
    else:
    no_space += 1
    with open("n.txt") as k:
    print(k.read())


    Output:



    ASDFSFDGHJEQ WRJIJSADFJSDF JJDSFJIDFJGIJ SRGJSDJFIDJFGG
    ASDFJSDGFIJSA DFJSDFJJDSFJIDF JGIJSRGJSDJFIDJ FGSADFJSDFJJ DSFJIDFJGIJK
    SADFJ SDFJJDSFJIDFJG IJSRGJSDJFIDJ FGSADFJSDFJJDS FJIDFJGIJSRG JSDJFIDJF
    SDFJG IKDSFGOROHPTLPASDMKFGD OKRAMGSADFJSDF JJDSFJIDFJGI JSRGJSDJFIDJFG





    share|improve this answer















    You can read a file line wise, write each line character-wise in a new file and insert spaces randomly:



    Create demo file without spaces:



    with open("t.txt","w") as f:
    f.write("""ASDFSFDGHJEQWRJIJG
    ASDFJSDGFIJ
    SADFJSDFJJDSFJIDFJGIJSRGJSDJFIDJFG
    SDFJGIKDSFGOROHPTLPASDMKFGDOKRAMGO""")


    Read and rewrite demo file:



    import random
    max_no_space = 9 # if max sequence length without space
    no_space = 0
    with open("t.txt","r") as f, open("n.txt","w") as w:
    for line in f:
    for c in line:
    w.write(c)
    if random.randint(1,6) == 1 or no_space >= max_no_space:
    w.write(" ")
    no_space = 0
    else:
    no_space += 1
    with open("n.txt") as k:
    print(k.read())


    Output:



    ASDF SFD GHJEQWRJIJG 
    A SDFJ SDG FIJ
    SADFJSD FJ JDSFJIDFJG I JSRGJSDJ FIDJFG


    The pattern of spaces is random. You can influence it by settin max_no_spaces or remove the randomness to split after max_no_spaces all the time




    Edit:



    This way of writing 1 character at a time if you need to read 200+ en block is not very economic, you can do it with the same code like so:



    with open("t.txt","w") as f:
    f.write("""ASDFSFDGHJEQWRJIJSADFJSDFJJDSFJIDFJGIJSRGJSDJFIDJFGG
    ASDFJSDGFIJSADFJSDFJJDSFJIDFJGIJSRGJSDJFIDJFGSADFJSDFJJDSFJIDFJGIJK
    SADFJSDFJJDSFJIDFJGIJSRGJSDJFIDJFGSADFJSDFJJDSFJIDFJGIJSRGJSDJFIDJF
    SDFJGIKDSFGOROHPTLPASDMKFGDOKRAMGSADFJSDFJJDSFJIDFJGIJSRGJSDJFIDJFG""")


    import random
    min_no_space = 10
    max_no_space = 20 # if max sequence length without space
    no_space = 0
    with open("t.txt","r") as f, open("n.txt","w") as w:
    for line in f:
    for c in line:
    w.write(c)
    if no_space > min_no_space:
    if random.randint(1,6) == 1 or no_space >= max_no_space:
    w.write(" ")
    no_space = 0
    else:
    no_space += 1
    with open("n.txt") as k:
    print(k.read())


    Output:



    ASDFSFDGHJEQ WRJIJSADFJSDF JJDSFJIDFJGIJ SRGJSDJFIDJFGG
    ASDFJSDGFIJSA DFJSDFJJDSFJIDF JGIJSRGJSDJFIDJ FGSADFJSDFJJ DSFJIDFJGIJK
    SADFJ SDFJJDSFJIDFJG IJSRGJSDJFIDJ FGSADFJSDFJJDS FJIDFJGIJSRG JSDJFIDJF
    SDFJG IKDSFGOROHPTLPASDMKFGD OKRAMGSADFJSDF JJDSFJIDFJGI JSRGJSDJFIDJFG






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 14 '18 at 19:45

























    answered Nov 13 '18 at 20:30









    Patrick ArtnerPatrick Artner

    23.4k62343




    23.4k62343












    • Thanks a million. I really appreciate it!

      – Alpa Luca
      Nov 14 '18 at 16:43











    • Is there a way to set the minimum sequence length without spaces? I am trying to get sequences that range from about 200-500 base pairs in length and the code is great I just need to set a minimum length before it inserts a space ( no single base pairs alone). Your help is so greatly appreciated! Thanks so much!!!

      – Alpa Luca
      Nov 14 '18 at 16:54











    • @AlpaLuca see edit

      – Patrick Artner
      Nov 14 '18 at 17:27











    • THANKS SO MUCH!!! LIFE SAVER!!! THANKS AGAIN!

      – Alpa Luca
      Nov 14 '18 at 19:35

















    • Thanks a million. I really appreciate it!

      – Alpa Luca
      Nov 14 '18 at 16:43











    • Is there a way to set the minimum sequence length without spaces? I am trying to get sequences that range from about 200-500 base pairs in length and the code is great I just need to set a minimum length before it inserts a space ( no single base pairs alone). Your help is so greatly appreciated! Thanks so much!!!

      – Alpa Luca
      Nov 14 '18 at 16:54











    • @AlpaLuca see edit

      – Patrick Artner
      Nov 14 '18 at 17:27











    • THANKS SO MUCH!!! LIFE SAVER!!! THANKS AGAIN!

      – Alpa Luca
      Nov 14 '18 at 19:35
















    Thanks a million. I really appreciate it!

    – Alpa Luca
    Nov 14 '18 at 16:43





    Thanks a million. I really appreciate it!

    – Alpa Luca
    Nov 14 '18 at 16:43













    Is there a way to set the minimum sequence length without spaces? I am trying to get sequences that range from about 200-500 base pairs in length and the code is great I just need to set a minimum length before it inserts a space ( no single base pairs alone). Your help is so greatly appreciated! Thanks so much!!!

    – Alpa Luca
    Nov 14 '18 at 16:54





    Is there a way to set the minimum sequence length without spaces? I am trying to get sequences that range from about 200-500 base pairs in length and the code is great I just need to set a minimum length before it inserts a space ( no single base pairs alone). Your help is so greatly appreciated! Thanks so much!!!

    – Alpa Luca
    Nov 14 '18 at 16:54













    @AlpaLuca see edit

    – Patrick Artner
    Nov 14 '18 at 17:27





    @AlpaLuca see edit

    – Patrick Artner
    Nov 14 '18 at 17:27













    THANKS SO MUCH!!! LIFE SAVER!!! THANKS AGAIN!

    – Alpa Luca
    Nov 14 '18 at 19:35





    THANKS SO MUCH!!! LIFE SAVER!!! THANKS AGAIN!

    – Alpa Luca
    Nov 14 '18 at 19:35













    0














    If you want to split your DNA fixed amount of times (10 in my example) here's what you could try:



    import random

    DNA = 'AAACCCHTHTHDAFHDSAFJANFAJDSNFADKFAFJ'
    splitted_DNA = ''

    for split_idx in sorted(random.sample(range(len(DNA)), 10)):
    splitted_DNA += DNA[len(splitted_DNA)-splitted_DNA.count(' ') :split_idx] + ' '
    splitted_DNA += DNA[split_idx:]

    print(splitted_DNA) # -> AAACCCHT HTH D AF HD SA F JANFAJDSNFA DK FAFJ





    share|improve this answer























    • Thanks so much for your time!! I appreciate it!

      – Alpa Luca
      Nov 14 '18 at 16:43















    0














    If you want to split your DNA fixed amount of times (10 in my example) here's what you could try:



    import random

    DNA = 'AAACCCHTHTHDAFHDSAFJANFAJDSNFADKFAFJ'
    splitted_DNA = ''

    for split_idx in sorted(random.sample(range(len(DNA)), 10)):
    splitted_DNA += DNA[len(splitted_DNA)-splitted_DNA.count(' ') :split_idx] + ' '
    splitted_DNA += DNA[split_idx:]

    print(splitted_DNA) # -> AAACCCHT HTH D AF HD SA F JANFAJDSNFA DK FAFJ





    share|improve this answer























    • Thanks so much for your time!! I appreciate it!

      – Alpa Luca
      Nov 14 '18 at 16:43













    0












    0








    0







    If you want to split your DNA fixed amount of times (10 in my example) here's what you could try:



    import random

    DNA = 'AAACCCHTHTHDAFHDSAFJANFAJDSNFADKFAFJ'
    splitted_DNA = ''

    for split_idx in sorted(random.sample(range(len(DNA)), 10)):
    splitted_DNA += DNA[len(splitted_DNA)-splitted_DNA.count(' ') :split_idx] + ' '
    splitted_DNA += DNA[split_idx:]

    print(splitted_DNA) # -> AAACCCHT HTH D AF HD SA F JANFAJDSNFA DK FAFJ





    share|improve this answer













    If you want to split your DNA fixed amount of times (10 in my example) here's what you could try:



    import random

    DNA = 'AAACCCHTHTHDAFHDSAFJANFAJDSNFADKFAFJ'
    splitted_DNA = ''

    for split_idx in sorted(random.sample(range(len(DNA)), 10)):
    splitted_DNA += DNA[len(splitted_DNA)-splitted_DNA.count(' ') :split_idx] + ' '
    splitted_DNA += DNA[split_idx:]

    print(splitted_DNA) # -> AAACCCHT HTH D AF HD SA F JANFAJDSNFA DK FAFJ






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 13 '18 at 20:31









    Filip MłynarskiFilip Młynarski

    1,6971412




    1,6971412












    • Thanks so much for your time!! I appreciate it!

      – Alpa Luca
      Nov 14 '18 at 16:43

















    • Thanks so much for your time!! I appreciate it!

      – Alpa Luca
      Nov 14 '18 at 16:43
















    Thanks so much for your time!! I appreciate it!

    – Alpa Luca
    Nov 14 '18 at 16:43





    Thanks so much for your time!! I appreciate it!

    – Alpa Luca
    Nov 14 '18 at 16:43











    0














    import random

    with open('source', 'r') as in_file:
    with open('dest', 'w') as out_file:
    for line in in_file:
    newLine = ''.join(map(lambda x:x+' '*random.randint(0,1), line)).strip() + 'n'
    out_file.write(newLine)


    Since you mentioned being new, I'll try to explain



    • I'm writing the new sequences to another file for precaution. It's
      not safe to write to the file you are reading from.


    • The with constructor is so that you don't need to explicitly close
      the file you opened.


    • Files can be read line by line using for loop.


    • ''.join() converts a list to a string.


    • map() applies a function to every element of a list and returns the
      results as a new list.


    • lambda is how you define a function without naming it. lambda x:
      2*x
      doubles the number you feed it.


    • x + ' ' * 3 adds 3 spaces after x. random.randint(0, 1) returns
      either 1 or 0. So I'm randomly selecting if I'll add a space after
      each character or not. If the random.randint() returns 0, 0 spaces are added.






    share|improve this answer




















    • 1





      thanks so much!!

      – Alpa Luca
      Nov 14 '18 at 16:43















    0














    import random

    with open('source', 'r') as in_file:
    with open('dest', 'w') as out_file:
    for line in in_file:
    newLine = ''.join(map(lambda x:x+' '*random.randint(0,1), line)).strip() + 'n'
    out_file.write(newLine)


    Since you mentioned being new, I'll try to explain



    • I'm writing the new sequences to another file for precaution. It's
      not safe to write to the file you are reading from.


    • The with constructor is so that you don't need to explicitly close
      the file you opened.


    • Files can be read line by line using for loop.


    • ''.join() converts a list to a string.


    • map() applies a function to every element of a list and returns the
      results as a new list.


    • lambda is how you define a function without naming it. lambda x:
      2*x
      doubles the number you feed it.


    • x + ' ' * 3 adds 3 spaces after x. random.randint(0, 1) returns
      either 1 or 0. So I'm randomly selecting if I'll add a space after
      each character or not. If the random.randint() returns 0, 0 spaces are added.






    share|improve this answer




















    • 1





      thanks so much!!

      – Alpa Luca
      Nov 14 '18 at 16:43













    0












    0








    0







    import random

    with open('source', 'r') as in_file:
    with open('dest', 'w') as out_file:
    for line in in_file:
    newLine = ''.join(map(lambda x:x+' '*random.randint(0,1), line)).strip() + 'n'
    out_file.write(newLine)


    Since you mentioned being new, I'll try to explain



    • I'm writing the new sequences to another file for precaution. It's
      not safe to write to the file you are reading from.


    • The with constructor is so that you don't need to explicitly close
      the file you opened.


    • Files can be read line by line using for loop.


    • ''.join() converts a list to a string.


    • map() applies a function to every element of a list and returns the
      results as a new list.


    • lambda is how you define a function without naming it. lambda x:
      2*x
      doubles the number you feed it.


    • x + ' ' * 3 adds 3 spaces after x. random.randint(0, 1) returns
      either 1 or 0. So I'm randomly selecting if I'll add a space after
      each character or not. If the random.randint() returns 0, 0 spaces are added.






    share|improve this answer















    import random

    with open('source', 'r') as in_file:
    with open('dest', 'w') as out_file:
    for line in in_file:
    newLine = ''.join(map(lambda x:x+' '*random.randint(0,1), line)).strip() + 'n'
    out_file.write(newLine)


    Since you mentioned being new, I'll try to explain



    • I'm writing the new sequences to another file for precaution. It's
      not safe to write to the file you are reading from.


    • The with constructor is so that you don't need to explicitly close
      the file you opened.


    • Files can be read line by line using for loop.


    • ''.join() converts a list to a string.


    • map() applies a function to every element of a list and returns the
      results as a new list.


    • lambda is how you define a function without naming it. lambda x:
      2*x
      doubles the number you feed it.


    • x + ' ' * 3 adds 3 spaces after x. random.randint(0, 1) returns
      either 1 or 0. So I'm randomly selecting if I'll add a space after
      each character or not. If the random.randint() returns 0, 0 spaces are added.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 13 '18 at 20:43

























    answered Nov 13 '18 at 20:35









    Aravind VogguAravind Voggu

    791413




    791413







    • 1





      thanks so much!!

      – Alpa Luca
      Nov 14 '18 at 16:43












    • 1





      thanks so much!!

      – Alpa Luca
      Nov 14 '18 at 16:43







    1




    1





    thanks so much!!

    – Alpa Luca
    Nov 14 '18 at 16:43





    thanks so much!!

    – Alpa Luca
    Nov 14 '18 at 16:43











    0














    You can toss a coin after each character whether to add space there or not.
    This function takes string as input and returns output with space inserted at random places.



    def insert_random_spaces(str):
    from random import randint
    output_string = "".join([x+randint(0,1)*" " for x in str])
    return output_string





    share|improve this answer





























      0














      You can toss a coin after each character whether to add space there or not.
      This function takes string as input and returns output with space inserted at random places.



      def insert_random_spaces(str):
      from random import randint
      output_string = "".join([x+randint(0,1)*" " for x in str])
      return output_string





      share|improve this answer



























        0












        0








        0







        You can toss a coin after each character whether to add space there or not.
        This function takes string as input and returns output with space inserted at random places.



        def insert_random_spaces(str):
        from random import randint
        output_string = "".join([x+randint(0,1)*" " for x in str])
        return output_string





        share|improve this answer















        You can toss a coin after each character whether to add space there or not.
        This function takes string as input and returns output with space inserted at random places.



        def insert_random_spaces(str):
        from random import randint
        output_string = "".join([x+randint(0,1)*" " for x in str])
        return output_string






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 13 '18 at 20:56

























        answered Nov 13 '18 at 20:48









        lateBloomerlateBloomer

        13




        13



























            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%2f53288893%2fhow-to-insert-random-spaces-in-txt-file%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







            這個網誌中的熱門文章

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

            Node.js Script on GitHub Pages or Amazon S3

            Museum of Modern and Contemporary Art of Trento and Rovereto