Sunday 5 November 2017

Python: put each line of words in a list

problem:



I
am trying to append words in the animals.txt into myList. The
animals.txt file looks like something
below.



code
tried:




opena
= open('animals.txt')
aread = opena.read()
myList = []

for line in aread:

myList.append(line.split("\n"))

print(myList)



animals.txt



COW
CHICKEN
DOG
RAT
MOUSE


expected
output for
myList:




myList
= ['COW', 'CHICKEN', 'DOG', 'RAT',
'MOUSE']

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...