Writing list of lists to a text file, one list at a time











up vote
0
down vote

favorite












I have the code below:



data=[(24, 'Sale', 0, 15), (16, 'Buy', 18, 0)]
with open('txt_file.txt', 'w') as x:
for sub_list in data:
for item in sub_list: #Since attempt to write whole giv error
print(item)
x.write(str(item) + ' ')


How can I get to extract one list from 'data', which is a list of lists, write it in one line and move to next line before extracting the other list? I need the text file to look like:



24 Sale 0 15
16 Buy 18 0









share|improve this question


















  • 2




    What's wrong with your current code?
    – scnerd
    Nov 7 at 13:31






  • 1




    missing a linefeed, basically
    – Jean-François Fabre
    Nov 7 at 13:49

















up vote
0
down vote

favorite












I have the code below:



data=[(24, 'Sale', 0, 15), (16, 'Buy', 18, 0)]
with open('txt_file.txt', 'w') as x:
for sub_list in data:
for item in sub_list: #Since attempt to write whole giv error
print(item)
x.write(str(item) + ' ')


How can I get to extract one list from 'data', which is a list of lists, write it in one line and move to next line before extracting the other list? I need the text file to look like:



24 Sale 0 15
16 Buy 18 0









share|improve this question


















  • 2




    What's wrong with your current code?
    – scnerd
    Nov 7 at 13:31






  • 1




    missing a linefeed, basically
    – Jean-François Fabre
    Nov 7 at 13:49















up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have the code below:



data=[(24, 'Sale', 0, 15), (16, 'Buy', 18, 0)]
with open('txt_file.txt', 'w') as x:
for sub_list in data:
for item in sub_list: #Since attempt to write whole giv error
print(item)
x.write(str(item) + ' ')


How can I get to extract one list from 'data', which is a list of lists, write it in one line and move to next line before extracting the other list? I need the text file to look like:



24 Sale 0 15
16 Buy 18 0









share|improve this question













I have the code below:



data=[(24, 'Sale', 0, 15), (16, 'Buy', 18, 0)]
with open('txt_file.txt', 'w') as x:
for sub_list in data:
for item in sub_list: #Since attempt to write whole giv error
print(item)
x.write(str(item) + ' ')


How can I get to extract one list from 'data', which is a list of lists, write it in one line and move to next line before extracting the other list? I need the text file to look like:



24 Sale 0 15
16 Buy 18 0






python list tuples






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 7 at 13:30









pro1991

114




114








  • 2




    What's wrong with your current code?
    – scnerd
    Nov 7 at 13:31






  • 1




    missing a linefeed, basically
    – Jean-François Fabre
    Nov 7 at 13:49
















  • 2




    What's wrong with your current code?
    – scnerd
    Nov 7 at 13:31






  • 1




    missing a linefeed, basically
    – Jean-François Fabre
    Nov 7 at 13:49










2




2




What's wrong with your current code?
– scnerd
Nov 7 at 13:31




What's wrong with your current code?
– scnerd
Nov 7 at 13:31




1




1




missing a linefeed, basically
– Jean-François Fabre
Nov 7 at 13:49






missing a linefeed, basically
– Jean-François Fabre
Nov 7 at 13:49














5 Answers
5






active

oldest

votes

















up vote
3
down vote



accepted










I think you could just write a newline just after exiting the inner loop, and your code would work (even if the last item would have spaces after it).



for item in sub_list:  #Since attempt to write whole giv error
x.write(str(item) + ' ')
x.write('n')


However, built-in csv module handles list of strings, integers, whatever, automatically (also handles strings with spaces in it by quoting them):



data=[(24, 'Sale', 0, 15), (16, 'Buy', 18, 0)]
with open('txt_file.txt', 'w', newline="") as x:
csv.writer(x,delimiter=" ").writerows(data)


the writerows method is able to write a collection of tuples or lists. Exactly what you need. The only difference is that csv cannot separate the data by more than one char (multi-char separators aren't allowed). You'll have to make do with 1 space.






share|improve this answer























  • Very helpful exiting the loop does the trick.
    – pro1991
    Nov 11 at 17:45










  • you can accept the answer if it solved your problem.
    – Jean-François Fabre
    Nov 11 at 18:51


















up vote
2
down vote













The most concise way to do this would be the following:



with open('txt_file.txt', 'w') as x:
x.write('n'.join(' '.join(map(str, row)) for row in data))





share|improve this answer























  • well, I don't think it works with OP data.
    – Jean-François Fabre
    Nov 7 at 13:37










  • Oh, forgot to format things...
    – scnerd
    Nov 7 at 13:44










  • yeah, that should work
    – Jean-François Fabre
    Nov 7 at 13:47










  • But it's not elegant, and your csv solution handles the nitty gritty crap better
    – scnerd
    Nov 7 at 13:57










  • only a raw solution is able to create a multi-spaced separator, that said.
    – Jean-François Fabre
    Nov 7 at 13:57


















up vote
0
down vote













try this:



with open('txt_file.txt','wt') as f:
[f.write(' '.join([str(it) for it in item])+'n') for item in data]





share|improve this answer























  • don't use comprehension just for looping. besides it doesn't work with OP data.
    – Jean-François Fabre
    Nov 7 at 13:38












  • fixed for OP's case
    – vencaslac
    Nov 7 at 14:14










  • please test your solution, because it's not working properly either.
    – Jean-François Fabre
    Nov 7 at 14:15










  • fixed again, but i agree it's becoming more and more convoluted
    – vencaslac
    Nov 7 at 14:31










  • at least it works. Consider f.writelines instead of a comprehension. And pass the generator expression to writelines. Much better than using a comprehsnion just for side effect
    – Jean-François Fabre
    Nov 7 at 14:41


















up vote
-1
down vote













Mine approach is based on your idea with a bit of adjustments (1. use format template so you can adjust final view and 2) using unpacking.) Final code looks like, apart make proper format template:



data=[(24, 'Sale', 0, 15), (16, 'Buy', 18, 0)]
FMT = '{} {} {}'

with open('txt_file.txt', 'w') as x:
for item in data:
record = FMT.format(*item)
print(record)
x.write(record + 'n')





share|improve this answer























  • FMT = '{} {} {}'.format({}, {}, {}) ??? why not just FMT = "{} {} {}" ? plus have you tested your code?
    – Jean-François Fabre
    Nov 7 at 13:48












  • good point. not fully.
    – Сергей Николаевич
    Nov 7 at 13:53










  • ahem. There are 4 arguments, not 3. why not including the linefeed in your format?
    – Jean-François Fabre
    Nov 7 at 13:56












  • that would write properly but not print if not specify end. After all idea still the same buy using format template and unpacking.
    – Сергей Николаевич
    Nov 7 at 14:01


















up vote
-2
down vote













"n" does the trick:



data=[(24, 'Sale', 0, 15), (16, 'Buy', 18, 0)]
with open('txt_file.txt', 'w') as x:
for sub_list in data:
for item in sub_list: #Since attempt to write whole giv error
print(item)
x.write(str(item) + ' ')
x.write("n")





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',
    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%2f53190443%2fwriting-list-of-lists-to-a-text-file-one-list-at-a-time%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    5 Answers
    5






    active

    oldest

    votes








    5 Answers
    5






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    3
    down vote



    accepted










    I think you could just write a newline just after exiting the inner loop, and your code would work (even if the last item would have spaces after it).



    for item in sub_list:  #Since attempt to write whole giv error
    x.write(str(item) + ' ')
    x.write('n')


    However, built-in csv module handles list of strings, integers, whatever, automatically (also handles strings with spaces in it by quoting them):



    data=[(24, 'Sale', 0, 15), (16, 'Buy', 18, 0)]
    with open('txt_file.txt', 'w', newline="") as x:
    csv.writer(x,delimiter=" ").writerows(data)


    the writerows method is able to write a collection of tuples or lists. Exactly what you need. The only difference is that csv cannot separate the data by more than one char (multi-char separators aren't allowed). You'll have to make do with 1 space.






    share|improve this answer























    • Very helpful exiting the loop does the trick.
      – pro1991
      Nov 11 at 17:45










    • you can accept the answer if it solved your problem.
      – Jean-François Fabre
      Nov 11 at 18:51















    up vote
    3
    down vote



    accepted










    I think you could just write a newline just after exiting the inner loop, and your code would work (even if the last item would have spaces after it).



    for item in sub_list:  #Since attempt to write whole giv error
    x.write(str(item) + ' ')
    x.write('n')


    However, built-in csv module handles list of strings, integers, whatever, automatically (also handles strings with spaces in it by quoting them):



    data=[(24, 'Sale', 0, 15), (16, 'Buy', 18, 0)]
    with open('txt_file.txt', 'w', newline="") as x:
    csv.writer(x,delimiter=" ").writerows(data)


    the writerows method is able to write a collection of tuples or lists. Exactly what you need. The only difference is that csv cannot separate the data by more than one char (multi-char separators aren't allowed). You'll have to make do with 1 space.






    share|improve this answer























    • Very helpful exiting the loop does the trick.
      – pro1991
      Nov 11 at 17:45










    • you can accept the answer if it solved your problem.
      – Jean-François Fabre
      Nov 11 at 18:51













    up vote
    3
    down vote



    accepted







    up vote
    3
    down vote



    accepted






    I think you could just write a newline just after exiting the inner loop, and your code would work (even if the last item would have spaces after it).



    for item in sub_list:  #Since attempt to write whole giv error
    x.write(str(item) + ' ')
    x.write('n')


    However, built-in csv module handles list of strings, integers, whatever, automatically (also handles strings with spaces in it by quoting them):



    data=[(24, 'Sale', 0, 15), (16, 'Buy', 18, 0)]
    with open('txt_file.txt', 'w', newline="") as x:
    csv.writer(x,delimiter=" ").writerows(data)


    the writerows method is able to write a collection of tuples or lists. Exactly what you need. The only difference is that csv cannot separate the data by more than one char (multi-char separators aren't allowed). You'll have to make do with 1 space.






    share|improve this answer














    I think you could just write a newline just after exiting the inner loop, and your code would work (even if the last item would have spaces after it).



    for item in sub_list:  #Since attempt to write whole giv error
    x.write(str(item) + ' ')
    x.write('n')


    However, built-in csv module handles list of strings, integers, whatever, automatically (also handles strings with spaces in it by quoting them):



    data=[(24, 'Sale', 0, 15), (16, 'Buy', 18, 0)]
    with open('txt_file.txt', 'w', newline="") as x:
    csv.writer(x,delimiter=" ").writerows(data)


    the writerows method is able to write a collection of tuples or lists. Exactly what you need. The only difference is that csv cannot separate the data by more than one char (multi-char separators aren't allowed). You'll have to make do with 1 space.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 7 at 13:47









    RoadRunner

    8,94831138




    8,94831138










    answered Nov 7 at 13:35









    Jean-François Fabre

    97.7k950107




    97.7k950107












    • Very helpful exiting the loop does the trick.
      – pro1991
      Nov 11 at 17:45










    • you can accept the answer if it solved your problem.
      – Jean-François Fabre
      Nov 11 at 18:51


















    • Very helpful exiting the loop does the trick.
      – pro1991
      Nov 11 at 17:45










    • you can accept the answer if it solved your problem.
      – Jean-François Fabre
      Nov 11 at 18:51
















    Very helpful exiting the loop does the trick.
    – pro1991
    Nov 11 at 17:45




    Very helpful exiting the loop does the trick.
    – pro1991
    Nov 11 at 17:45












    you can accept the answer if it solved your problem.
    – Jean-François Fabre
    Nov 11 at 18:51




    you can accept the answer if it solved your problem.
    – Jean-François Fabre
    Nov 11 at 18:51












    up vote
    2
    down vote













    The most concise way to do this would be the following:



    with open('txt_file.txt', 'w') as x:
    x.write('n'.join(' '.join(map(str, row)) for row in data))





    share|improve this answer























    • well, I don't think it works with OP data.
      – Jean-François Fabre
      Nov 7 at 13:37










    • Oh, forgot to format things...
      – scnerd
      Nov 7 at 13:44










    • yeah, that should work
      – Jean-François Fabre
      Nov 7 at 13:47










    • But it's not elegant, and your csv solution handles the nitty gritty crap better
      – scnerd
      Nov 7 at 13:57










    • only a raw solution is able to create a multi-spaced separator, that said.
      – Jean-François Fabre
      Nov 7 at 13:57















    up vote
    2
    down vote













    The most concise way to do this would be the following:



    with open('txt_file.txt', 'w') as x:
    x.write('n'.join(' '.join(map(str, row)) for row in data))





    share|improve this answer























    • well, I don't think it works with OP data.
      – Jean-François Fabre
      Nov 7 at 13:37










    • Oh, forgot to format things...
      – scnerd
      Nov 7 at 13:44










    • yeah, that should work
      – Jean-François Fabre
      Nov 7 at 13:47










    • But it's not elegant, and your csv solution handles the nitty gritty crap better
      – scnerd
      Nov 7 at 13:57










    • only a raw solution is able to create a multi-spaced separator, that said.
      – Jean-François Fabre
      Nov 7 at 13:57













    up vote
    2
    down vote










    up vote
    2
    down vote









    The most concise way to do this would be the following:



    with open('txt_file.txt', 'w') as x:
    x.write('n'.join(' '.join(map(str, row)) for row in data))





    share|improve this answer














    The most concise way to do this would be the following:



    with open('txt_file.txt', 'w') as x:
    x.write('n'.join(' '.join(map(str, row)) for row in data))






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 7 at 13:44

























    answered Nov 7 at 13:32









    scnerd

    3,0011820




    3,0011820












    • well, I don't think it works with OP data.
      – Jean-François Fabre
      Nov 7 at 13:37










    • Oh, forgot to format things...
      – scnerd
      Nov 7 at 13:44










    • yeah, that should work
      – Jean-François Fabre
      Nov 7 at 13:47










    • But it's not elegant, and your csv solution handles the nitty gritty crap better
      – scnerd
      Nov 7 at 13:57










    • only a raw solution is able to create a multi-spaced separator, that said.
      – Jean-François Fabre
      Nov 7 at 13:57


















    • well, I don't think it works with OP data.
      – Jean-François Fabre
      Nov 7 at 13:37










    • Oh, forgot to format things...
      – scnerd
      Nov 7 at 13:44










    • yeah, that should work
      – Jean-François Fabre
      Nov 7 at 13:47










    • But it's not elegant, and your csv solution handles the nitty gritty crap better
      – scnerd
      Nov 7 at 13:57










    • only a raw solution is able to create a multi-spaced separator, that said.
      – Jean-François Fabre
      Nov 7 at 13:57
















    well, I don't think it works with OP data.
    – Jean-François Fabre
    Nov 7 at 13:37




    well, I don't think it works with OP data.
    – Jean-François Fabre
    Nov 7 at 13:37












    Oh, forgot to format things...
    – scnerd
    Nov 7 at 13:44




    Oh, forgot to format things...
    – scnerd
    Nov 7 at 13:44












    yeah, that should work
    – Jean-François Fabre
    Nov 7 at 13:47




    yeah, that should work
    – Jean-François Fabre
    Nov 7 at 13:47












    But it's not elegant, and your csv solution handles the nitty gritty crap better
    – scnerd
    Nov 7 at 13:57




    But it's not elegant, and your csv solution handles the nitty gritty crap better
    – scnerd
    Nov 7 at 13:57












    only a raw solution is able to create a multi-spaced separator, that said.
    – Jean-François Fabre
    Nov 7 at 13:57




    only a raw solution is able to create a multi-spaced separator, that said.
    – Jean-François Fabre
    Nov 7 at 13:57










    up vote
    0
    down vote













    try this:



    with open('txt_file.txt','wt') as f:
    [f.write(' '.join([str(it) for it in item])+'n') for item in data]





    share|improve this answer























    • don't use comprehension just for looping. besides it doesn't work with OP data.
      – Jean-François Fabre
      Nov 7 at 13:38












    • fixed for OP's case
      – vencaslac
      Nov 7 at 14:14










    • please test your solution, because it's not working properly either.
      – Jean-François Fabre
      Nov 7 at 14:15










    • fixed again, but i agree it's becoming more and more convoluted
      – vencaslac
      Nov 7 at 14:31










    • at least it works. Consider f.writelines instead of a comprehension. And pass the generator expression to writelines. Much better than using a comprehsnion just for side effect
      – Jean-François Fabre
      Nov 7 at 14:41















    up vote
    0
    down vote













    try this:



    with open('txt_file.txt','wt') as f:
    [f.write(' '.join([str(it) for it in item])+'n') for item in data]





    share|improve this answer























    • don't use comprehension just for looping. besides it doesn't work with OP data.
      – Jean-François Fabre
      Nov 7 at 13:38












    • fixed for OP's case
      – vencaslac
      Nov 7 at 14:14










    • please test your solution, because it's not working properly either.
      – Jean-François Fabre
      Nov 7 at 14:15










    • fixed again, but i agree it's becoming more and more convoluted
      – vencaslac
      Nov 7 at 14:31










    • at least it works. Consider f.writelines instead of a comprehension. And pass the generator expression to writelines. Much better than using a comprehsnion just for side effect
      – Jean-François Fabre
      Nov 7 at 14:41













    up vote
    0
    down vote










    up vote
    0
    down vote









    try this:



    with open('txt_file.txt','wt') as f:
    [f.write(' '.join([str(it) for it in item])+'n') for item in data]





    share|improve this answer














    try this:



    with open('txt_file.txt','wt') as f:
    [f.write(' '.join([str(it) for it in item])+'n') for item in data]






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 7 at 14:24

























    answered Nov 7 at 13:34









    vencaslac

    1,008217




    1,008217












    • don't use comprehension just for looping. besides it doesn't work with OP data.
      – Jean-François Fabre
      Nov 7 at 13:38












    • fixed for OP's case
      – vencaslac
      Nov 7 at 14:14










    • please test your solution, because it's not working properly either.
      – Jean-François Fabre
      Nov 7 at 14:15










    • fixed again, but i agree it's becoming more and more convoluted
      – vencaslac
      Nov 7 at 14:31










    • at least it works. Consider f.writelines instead of a comprehension. And pass the generator expression to writelines. Much better than using a comprehsnion just for side effect
      – Jean-François Fabre
      Nov 7 at 14:41


















    • don't use comprehension just for looping. besides it doesn't work with OP data.
      – Jean-François Fabre
      Nov 7 at 13:38












    • fixed for OP's case
      – vencaslac
      Nov 7 at 14:14










    • please test your solution, because it's not working properly either.
      – Jean-François Fabre
      Nov 7 at 14:15










    • fixed again, but i agree it's becoming more and more convoluted
      – vencaslac
      Nov 7 at 14:31










    • at least it works. Consider f.writelines instead of a comprehension. And pass the generator expression to writelines. Much better than using a comprehsnion just for side effect
      – Jean-François Fabre
      Nov 7 at 14:41
















    don't use comprehension just for looping. besides it doesn't work with OP data.
    – Jean-François Fabre
    Nov 7 at 13:38






    don't use comprehension just for looping. besides it doesn't work with OP data.
    – Jean-François Fabre
    Nov 7 at 13:38














    fixed for OP's case
    – vencaslac
    Nov 7 at 14:14




    fixed for OP's case
    – vencaslac
    Nov 7 at 14:14












    please test your solution, because it's not working properly either.
    – Jean-François Fabre
    Nov 7 at 14:15




    please test your solution, because it's not working properly either.
    – Jean-François Fabre
    Nov 7 at 14:15












    fixed again, but i agree it's becoming more and more convoluted
    – vencaslac
    Nov 7 at 14:31




    fixed again, but i agree it's becoming more and more convoluted
    – vencaslac
    Nov 7 at 14:31












    at least it works. Consider f.writelines instead of a comprehension. And pass the generator expression to writelines. Much better than using a comprehsnion just for side effect
    – Jean-François Fabre
    Nov 7 at 14:41




    at least it works. Consider f.writelines instead of a comprehension. And pass the generator expression to writelines. Much better than using a comprehsnion just for side effect
    – Jean-François Fabre
    Nov 7 at 14:41










    up vote
    -1
    down vote













    Mine approach is based on your idea with a bit of adjustments (1. use format template so you can adjust final view and 2) using unpacking.) Final code looks like, apart make proper format template:



    data=[(24, 'Sale', 0, 15), (16, 'Buy', 18, 0)]
    FMT = '{} {} {}'

    with open('txt_file.txt', 'w') as x:
    for item in data:
    record = FMT.format(*item)
    print(record)
    x.write(record + 'n')





    share|improve this answer























    • FMT = '{} {} {}'.format({}, {}, {}) ??? why not just FMT = "{} {} {}" ? plus have you tested your code?
      – Jean-François Fabre
      Nov 7 at 13:48












    • good point. not fully.
      – Сергей Николаевич
      Nov 7 at 13:53










    • ahem. There are 4 arguments, not 3. why not including the linefeed in your format?
      – Jean-François Fabre
      Nov 7 at 13:56












    • that would write properly but not print if not specify end. After all idea still the same buy using format template and unpacking.
      – Сергей Николаевич
      Nov 7 at 14:01















    up vote
    -1
    down vote













    Mine approach is based on your idea with a bit of adjustments (1. use format template so you can adjust final view and 2) using unpacking.) Final code looks like, apart make proper format template:



    data=[(24, 'Sale', 0, 15), (16, 'Buy', 18, 0)]
    FMT = '{} {} {}'

    with open('txt_file.txt', 'w') as x:
    for item in data:
    record = FMT.format(*item)
    print(record)
    x.write(record + 'n')





    share|improve this answer























    • FMT = '{} {} {}'.format({}, {}, {}) ??? why not just FMT = "{} {} {}" ? plus have you tested your code?
      – Jean-François Fabre
      Nov 7 at 13:48












    • good point. not fully.
      – Сергей Николаевич
      Nov 7 at 13:53










    • ahem. There are 4 arguments, not 3. why not including the linefeed in your format?
      – Jean-François Fabre
      Nov 7 at 13:56












    • that would write properly but not print if not specify end. After all idea still the same buy using format template and unpacking.
      – Сергей Николаевич
      Nov 7 at 14:01













    up vote
    -1
    down vote










    up vote
    -1
    down vote









    Mine approach is based on your idea with a bit of adjustments (1. use format template so you can adjust final view and 2) using unpacking.) Final code looks like, apart make proper format template:



    data=[(24, 'Sale', 0, 15), (16, 'Buy', 18, 0)]
    FMT = '{} {} {}'

    with open('txt_file.txt', 'w') as x:
    for item in data:
    record = FMT.format(*item)
    print(record)
    x.write(record + 'n')





    share|improve this answer














    Mine approach is based on your idea with a bit of adjustments (1. use format template so you can adjust final view and 2) using unpacking.) Final code looks like, apart make proper format template:



    data=[(24, 'Sale', 0, 15), (16, 'Buy', 18, 0)]
    FMT = '{} {} {}'

    with open('txt_file.txt', 'w') as x:
    for item in data:
    record = FMT.format(*item)
    print(record)
    x.write(record + 'n')






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 7 at 13:54

























    answered Nov 7 at 13:46









    Сергей Николаевич

    1046




    1046












    • FMT = '{} {} {}'.format({}, {}, {}) ??? why not just FMT = "{} {} {}" ? plus have you tested your code?
      – Jean-François Fabre
      Nov 7 at 13:48












    • good point. not fully.
      – Сергей Николаевич
      Nov 7 at 13:53










    • ahem. There are 4 arguments, not 3. why not including the linefeed in your format?
      – Jean-François Fabre
      Nov 7 at 13:56












    • that would write properly but not print if not specify end. After all idea still the same buy using format template and unpacking.
      – Сергей Николаевич
      Nov 7 at 14:01


















    • FMT = '{} {} {}'.format({}, {}, {}) ??? why not just FMT = "{} {} {}" ? plus have you tested your code?
      – Jean-François Fabre
      Nov 7 at 13:48












    • good point. not fully.
      – Сергей Николаевич
      Nov 7 at 13:53










    • ahem. There are 4 arguments, not 3. why not including the linefeed in your format?
      – Jean-François Fabre
      Nov 7 at 13:56












    • that would write properly but not print if not specify end. After all idea still the same buy using format template and unpacking.
      – Сергей Николаевич
      Nov 7 at 14:01
















    FMT = '{} {} {}'.format({}, {}, {}) ??? why not just FMT = "{} {} {}" ? plus have you tested your code?
    – Jean-François Fabre
    Nov 7 at 13:48






    FMT = '{} {} {}'.format({}, {}, {}) ??? why not just FMT = "{} {} {}" ? plus have you tested your code?
    – Jean-François Fabre
    Nov 7 at 13:48














    good point. not fully.
    – Сергей Николаевич
    Nov 7 at 13:53




    good point. not fully.
    – Сергей Николаевич
    Nov 7 at 13:53












    ahem. There are 4 arguments, not 3. why not including the linefeed in your format?
    – Jean-François Fabre
    Nov 7 at 13:56






    ahem. There are 4 arguments, not 3. why not including the linefeed in your format?
    – Jean-François Fabre
    Nov 7 at 13:56














    that would write properly but not print if not specify end. After all idea still the same buy using format template and unpacking.
    – Сергей Николаевич
    Nov 7 at 14:01




    that would write properly but not print if not specify end. After all idea still the same buy using format template and unpacking.
    – Сергей Николаевич
    Nov 7 at 14:01










    up vote
    -2
    down vote













    "n" does the trick:



    data=[(24, 'Sale', 0, 15), (16, 'Buy', 18, 0)]
    with open('txt_file.txt', 'w') as x:
    for sub_list in data:
    for item in sub_list: #Since attempt to write whole giv error
    print(item)
    x.write(str(item) + ' ')
    x.write("n")





    share|improve this answer



























      up vote
      -2
      down vote













      "n" does the trick:



      data=[(24, 'Sale', 0, 15), (16, 'Buy', 18, 0)]
      with open('txt_file.txt', 'w') as x:
      for sub_list in data:
      for item in sub_list: #Since attempt to write whole giv error
      print(item)
      x.write(str(item) + ' ')
      x.write("n")





      share|improve this answer

























        up vote
        -2
        down vote










        up vote
        -2
        down vote









        "n" does the trick:



        data=[(24, 'Sale', 0, 15), (16, 'Buy', 18, 0)]
        with open('txt_file.txt', 'w') as x:
        for sub_list in data:
        for item in sub_list: #Since attempt to write whole giv error
        print(item)
        x.write(str(item) + ' ')
        x.write("n")





        share|improve this answer














        "n" does the trick:



        data=[(24, 'Sale', 0, 15), (16, 'Buy', 18, 0)]
        with open('txt_file.txt', 'w') as x:
        for sub_list in data:
        for item in sub_list: #Since attempt to write whole giv error
        print(item)
        x.write(str(item) + ' ')
        x.write("n")






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 7 at 14:02

























        answered Nov 7 at 13:43









        Suhas NM

        875




        875






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53190443%2fwriting-list-of-lists-to-a-text-file-one-list-at-a-time%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







            這個網誌中的熱門文章

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud

            Zucchini