美文网首页
python逐行读取txt文件

python逐行读取txt文件

作者: 猴子喜 | 来源:发表于2018-12-06 15:46 被阅读0次

<shape_names.txt>

airplane
bathtub
bed
bench
bookshelf
bottle
bowl
car
chair
cone
cup

read

使用line.rstrip()

>>> file=open(('/shape_names.txt'))
>>> file
<_io.TextIOWrapper name='/shape_names.txt' mode='r' encoding='ANSI_X3.4-1968'>
>>> a=[line.rstrip() for line in file]
>>> a
['airplane', 'bathtub', 'bed', 'bench', 'bookshelf', 'bottle', 'bowl', 'car', 'chair', 'cone', 'cup', 'curtain', 'desk', 'door', 'dresser', 'flower_pot', 'glass_box', 'guitar', 'keyboard', 'lamp', 'laptop', 'mantel', 'monitor', 'night_stand', 'person', 'piano', 'plant', 'radio', 'range_hood', 'sink', 'sofa', 'stairs', 'stool', 'table', 'tent', 'toilet', 'tv_stand', 'vase', 'wardrobe', 'xbox']

相关文章

网友评论

      本文标题:python逐行读取txt文件

      本文链接:https://www.haomeiwen.com/subject/eoyocqtx.html