文件内容转换成列表:
with open('somefile','r') as f:
content = list(f)
print(content)
"""
['Hello\n', 'World\n', 'Python']
"""
去掉换行符
方式一
with open('somefile','r') as f:
content = f.read().splitlines()
print(content)
"""
['Hello', 'World', 'Python']
"""
方式二
with open('somefile','r') as f:
content = [line.rstrip('\n') for line in f]
print(content)
"""
['Hello', 'World', 'Python']
"""
其中,content结果都是去掉每一行行尾的换行符
去掉行首行尾的空白字符
with open('somefile','r') as f:
content = [line.strip() for line in f]
print(content)
引用自:https://www.cnblogs.com/Hello-python/articles/10201952.html
03-16
5026
05-05
774
01-18
1866