我正在尝试从网站中删除一些数据,下面是我设法获得的长字符串。
var playerlist=["Roger Federer", "Rainer Schuettler", "Dominik Hrbaty", "Thomas Muster", "Andy Roddick", "Nikolay Davydenko", "Tommy Haas", "Jarkko Nieminen", "Arnaud Clement", "Ivan Ljubicic", "David Ferrer", "Nicolas Massu", "Tommy Robredo", "Lleyton Hewitt", "Filippo Volandri", "Olivier Rochus", "Kevin Kim", "Juan Ignacio Chela", "Juan Carlos Ferrero", "Jimmy Connors", "Mikhail Youzhny", "Ruben Ramirez Hidalgo", "Rafael Nadal"]
上面不是javascript列表,它是一个String。
我想从此字符串创建所有玩家名称的列表。所以我必须提取“”之间的所有子串并将其添加到列表中。或者,如果我能以某种方式将此字符串转换为列表或数组,那就太棒了。
有人可以建议我们如何在python中执行此操作?
答案 0 :(得分:1)
您可以使用ast.literal_eval
>>> s = 'var playerlist=["Roger Federer", "Rainer Schuettler", "Dominik Hrbaty", "Thomas Muster", "Andy Roddick", "Nikolay Davydenko", "Tommy Haas", "Jarkko Nieminen", "Arnaud Clement", "Ivan Ljubicic", "David Ferrer", "Nicolas Massu", "Tommy Robredo", "Lleyton Hewitt", "Filippo Volandri", "Olivier Rochus", "Kevin Kim", "Juan Ignacio Chela", "Juan Carlos Ferrero", "Jimmy Connors", "Mikhail Youzhny", "Ruben Ramirez Hidalgo", "Rafael Nadal"]'
>>> import ast
>>> start = s.index('[')
>>> ast.literal_eval(s[start:])
['Roger Federer', 'Rainer Schuettler', 'Dominik Hrbaty', 'Thomas Muster', 'Andy Roddick', 'Nikolay Davydenko', 'Tommy Haas', 'Jarkko Nieminen', 'Arnaud Clement', 'Ivan Ljubicic', 'David Ferrer', 'Nicolas Massu', 'Tommy Robredo', 'Lleyton Hewitt', 'Filippo Volandri', 'Olivier Rochus', 'Kevin Kim', 'Juan Ignacio Chela', 'Juan Carlos Ferrero', 'Jimmy Connors', 'Mikhail Youzhny', 'Ruben Ramirez Hidalgo', 'Rafael Nadal']
正如史蒂夫在评论中提到的那样,最好使用json.loads
>>> import json
>>> json.loads(s[start:])
[u'Roger Federer', u'Rainer Schuettler', u'Dominik Hrbaty', u'Thomas Muster', u'Andy Roddick', u'Nikolay Davydenko', u'Tommy Haas', u'Jarkko Nieminen', u'Arnaud Clement', u'Ivan Ljubicic', u'David Ferrer', u'Nicolas Massu', u'Tommy Robredo', u'Lleyton Hewitt', u'Filippo Volandri', u'Olivier Rochus', u'Kevin Kim', u'Juan Ignacio Chela', u'Juan Carlos Ferrero', u'Jimmy Connors', u'Mikhail Youzhny', u'Ruben Ramirez Hidalgo', u'Rafael Nadal']