python txt to list code example
Example 1: how to put a text file into a list python
david
mary
john
with open('names.txt', 'r') as f:
myNames = [line.strip() for line in f]
['david','mary','john']
Example 2: each line in a text file into a list in Python
with open('file1.txt','r') as f:
listl=[]
for line in f:
strip_lines=line.strip()
listli=strip_lines.split()
print(listli)
m=listl.append(listli)
print(listl)
Example 3: python read text file to list
with open(filename) as f:
content = f.readlines()
content = [x.strip() for x in content]