Search code examples
pythonstringfile-read

Reading text file to array


I am trying to read a file into an array, my current implementation is only returning the first line of the .txt file

import re
def GetText(filename):
print('Opening file...')

text_file= open(filename,'r')
lines = text_file.readlines() #each line is appended to a list

with text_file:
    one_string= text_file.read().replace('\n', '')
print(one_string)

My question is: How do I read a text file to an array?


Solution

  • Instead of reading the whole file a line at atime why not read it all in one go and then split based on full stops (periods) in order to get sentences... ie:

    text_file= open(filename,'r')
    data=text_file.read()
    listOfSentences = data.split(".")