Using A For Loop To Print Each Item Of A List From An External File In Python
I am writing a program that reads a 2D list from a .txt file, and I'm trying to loop through the list, and print each item in it. I've used a for loop to loop through each item in
Solution 1:
Splitting on newlines and outputting in your format:
from ast import literal_eval
file_contents = file.readlines() #read the file as lines
for line in file_contents:
l = literal_eval(line) #convert the string to a list
print(''.join([v.ljust(10, ' ') for v in l])) #left justify and print
Post a Comment for "Using A For Loop To Print Each Item Of A List From An External File In Python"