我有一个txt文件,看起来像这样:
FSDAF HKLWF AAKEP
我怎样才能将其读入一个列表,使其看起来像这样: lst=['FSDAF'、'HKLWF'、'AAKEP'] 我做了类似的事情,但我无法摆脱这些新词:
file = open('dane.txt', 'r') lst = file.readlines()
它会显示如下新行:
['FSDAF\n', 'HKLWF\n', 'AAKEP\n']
你可以打开它。。。
with open('dane.txt', 'r') as file: ...
...然后(进入 with / as
with
as
content = file.read()
...然后将其拆分为一个列表:
myList = content.split('\n')
你可以用 .rstrip() :
.rstrip()
data = ['FSDAF\n', 'HKLWF\n', 'AAKEP\n'] print([line.rstrip() for line in data])