2

i have made a nested list and it works a print i need to write it to a text file but dont understand how to and have tried to do research and not found anything anyone help?

nestedList = [["Judge","01","02","03","04","05"],
             ["Couple A","10","06","03","04","05"]]
file = open("NewFile.txt","w")

for item in nestedList:
    print(":",item[0]," "*(8-len(item[0])),":",
          item[1]," "*(0-len(item[1])),":",
          item[2]," "*(0-len(item[2])),":",
          item[3]," "*(0-len(item[3])),":",
          item[4]," "*(0-len(item[4])),":",
          item[5]," "*(0-len(item[5])),":")

file.close()

Edited:

I've changed code to this:

    nestedList = [["Judge","01","02","03","04","05"],
             ["Couple A","10","06","03","04","05"]]
with open("NewFile.txt",'w') as outfile:
    for item in nestedList:
        (":",item[0]," "*(8-len(item[0])),":",
              item[1]," "*(0-len(item[1])),":",
              item[2]," "*(0-len(item[2])),":",
              item[3]," "*(0-len(item[3])),":",
              item[4]," "*(0-len(item[4])),":",
              item[5]," "*(0-len(item[5])),":")
outfile.close()

1 Answers1

0

The following code can help you write to file. Not sure what the expected output is. You can modify the write statement to print anything you like.

nestedList = [["Judge","01","02","03","04","05"],
             ["Couple A","10","06","03","04","05"]]

with open("myfile.txt",'w') as outfile:
    for i in nestedList:
        outfile.write(str(i))

UPDATE: The following code below writes to the file. (Writes everything after the print as wanted)

, is not accepted as it sees as multiple arguments. Instead + is used to concatenate strings, which then can be written.

nestedList = [["Judge","01","02","03","04","05"],
             ["Couple A","10","06","03","04","05"]]

with open("myfile.txt",'w') as outfile:
    for item in nestedList:
        outfile.write(":"+item[0] + " "*(8-len(item[0]))+":"+
          item[1]+" "*(0-len(item[1]))+":"+
          item[2]+" "*(0-len(item[2]))+":"+
          item[3]+" "*(0-len(item[3]))+":"+
          item[4]+" "*(0-len(item[4]))+":"+
          item[5]+" "*(0-len(item[5]))+":")

UPDATE 2:

If you want to format the code, you might want to look into formatting but here is a an example to make it look similar

nestedList = [["Judge","01","02","03","04","05"],
             ["Couple A","10","06","03","04","05"]]

with open("myfile.txt",'w') as outfile:
    for item in nestedList:
        outfile.write("\n:\t"+item[0] + " "*(8-len(item[0]))+":"+
          item[1]+" \t"*(0-len(item[1]))+":\t"+
          item[4]+" \t"*(0-len(item[4]))+":\t"+
          item[2]+" \t"*(0-len(item[2]))+":\t"+
          item[3]+" \t"*(0-len(item[3]))+":\t"+
          item[5]+" \t"*(0-len(item[5]))+":\t")
Maddy
  • 2,025
  • 5
  • 26
  • 59
  • I've edited and shown what I've done to use your code but I can't see anything to write to a file? – GraphicsLab Oct 04 '17 at 15:49
  • @GraphicsLab, If I understood it correctly, you want to write everything after `print` to a textfile. – Maddy Oct 04 '17 at 15:51
  • yeah in the format running with print so what comes out in the shell a table looking thing i want it to look like the table in the text doc – GraphicsLab Oct 04 '17 at 15:54
  • there a error on where the + is can you check please? – GraphicsLab Oct 04 '17 at 16:01
  • whats the error? I just ran it and works fine for me. – Maddy Oct 04 '17 at 16:02
  • what python are you using? and its saying the 8th + in if that helps – GraphicsLab Oct 04 '17 at 16:04
  • @GraphicsLab, try now. Just updated the code. Fat fingered myself while copying the code. – Maddy Oct 04 '17 at 16:05
  • its works now cheers can you help me get it back to the format my code prints at if you can ? as yours is all spaced out etc – GraphicsLab Oct 04 '17 at 16:08
  • @GraphicsLab, you might want to look into python formatting and playing with code a little bit for that. For example to remove the extra space, in start, you can get rid of the `\t`. `https://stackoverflow.com/questions/23835810/how-to-add-x-number-of-spaces-to-a-string` can help you with that. – Maddy Oct 04 '17 at 16:13
  • ive sorted it out mostly last quistion how can i put spaces before the number? – GraphicsLab Oct 04 '17 at 16:16