Matt Matt - 4 months ago 47
Python Question

Adding each character in a line of input file to list, and adding each list to another list after each line

Basically what I am trying to do is read in each character from each line into a list, and after each line, add that list into another list (one list per line in input file, each list containing all the individual characters of each line)

This is what I have so far but it doesnt seem to be working and I can't figure out why.

allseq = []
with open("input.txt", "r") as ins:
seq = []
for line in ins:
for ch in line:
if ins != "\n":
seq[:] = []



Strings in Python can be easily converted into literal lists of characters! Let's make a function.

def get_char_lists(file):
    with open(file) as f:
        return [list(line.strip()) for line in f.readlines()]

This opens a file for reading, reads all the lines, strips off extraneous whitespace, sticks a list of the characters into a list, and returns that last list.