Sunday, 30 July 2017

Each line an element in an array when reading text file in Python

When reading the text file, I'm using:



import io

with io.open('MasterList.txt', encoding='latin-1') as myfile:
data=myfile.read().replace('\n', '')


But printing something like data[0] gives me the first character instead of the first line. How can I make data be a list where each element is a line from the text file? Each line ends in \n. Do I have to somehow use that as the marker of where a new line begins?

No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...