Issue
I have a part of a program with the following code
file1 = [line.strip()for line in open(sometext.txt).readlines()]
print ((file1)[0])
and when the code is executed it gives me the whole contents of the txt file which is a very long sentence, how would I go about reading every letter and placing it in a list to index each character separately? I have used the list() function which seems to put the whole text file into a list and not each character.
Solution
With a read()
is enough. Plus. if you want to store the list without \n
and white spaces, you can use:
char_list = [ch for ch in open('test.txt').read() if ch != '\n' if ch != ' ']
You can remove the if
statements if you want to maintain them.
Answered By - Avión
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.