Python Practice › File Handling Programs › Program 24
Characters of a file into a list
Write a Python program to extract the characters (not spaces or newlines) of a file into a list.
Output
['s', 'q', 'a', 't', 'o', 'o', 'l', 's', 'p', 'y']
Approach
- Keep characters that are not whitespace.
Solution
with open("word.txt", "w") as f: f.write("sqa tools\npy\n") with open("word.txt") as f: print([ch for ch in f.read() if not ch.isspace()])Output
['s', 'q', 'a', 't', 'o', 'o', 'l', 's', 'p', 'y']
Learn the concept
New to this topic? Read the lesson, then come back.