'Convert text file to python list [duplicate]

I am trying to create a python list from text file and this is my try

my_file = open("Sample.txt", "r")
content_list = my_file.readlines().replace('\n','')
print(content_list)

Solved

mylist = open('Sample.txt', 'r').read().splitlines()
print(mylist)


Solution 1:[1]

You can strip method.

If strip doesn't work then follow below approach

Use str.splitlines() to split your document into lines without newlines; you can rejoin it if you want to:

doclines = doc.splitlines() doc_rejoined = ''.join(doclines)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Prasad