2018-07-02python 用了readlines后成为列表处理换行符‘\n’

>>> h = open('1.txt','r')

>>> word = h.readline()

>>> word'wo ai ni fql and nannan\n'

>>> word'wo ai ni fql and nannan\n'

>>> word = h.readlines()

>>> word['1\n', '2\n', '3\n', '4\n', '5\n', '6\n', '7']

直接用read发现list没有read属性

>>> word = word.read().splitlines()

Traceback (most recent call last): File "", line 1, inAttributeError: 'list' object has no attribute 'read'

直接用下面方法时候,生成位空列表,把所有内容都去除了

>>> word =[word.strip('\n') for line in h.readlines()]

>>> word

[]

于是,在网上搜到了下面的方法,可以使用

>>> word

['1\n', '2\n', '3\n', '4\n', '5\n', '6\n', '7']


>>> for i in range(0,len(word)):

...    word[i] = word[i].strip()

...

>>> word

['1', '2', '3', '4', '5', '6', '7']

>>>

你可能感兴趣的:(2018-07-02python 用了readlines后成为列表处理换行符‘\n’)