python将文件内容中的每一行加入到list

1.假设mytest.txt内容如下:

person
bicycle
car
motorcycle

2.读取程序:

import sys
result = []
with open('./mytest.txt') as f:
    lines = f.readlines()
    for line in lines:
        result.append(line.strip('\n'))
    print(result)

3.结果:

['person', 'bicycle', 'car', 'motorcycle']

你可能感兴趣的:(python将文件内容中的每一行加入到list)