我从HTML页面中提取了一组数据并复制到变量中.变量看起来像
names='''
      Apple
      Ball
      Cat'''
现在我想将每一行加入到列表中,以便我可以访问我想要的任何行.有没有办法在Python中做到这一点
var*_*unl 10
使用splitlines()按换行符和strip()拆分以删除不必要的空格.
>>> names='''
...       Apple
...       Ball
...       Cat'''
>>> names
'\n      Apple\n      Ball\n      Cat'
>>> names_list = [y for y in (x.strip() for x in names.splitlines()) if y]
>>> # if x.strip() is used to remove empty lines
>>> names_list
['Apple', 'Ball', 'Cat']