从文本文件加载列表

时间:2014-03-17 21:40:21

标签: python list parsing

我有一个包含以下内容的文本文件:

['Joakim Noah', 'Wilson Chandler', 'Michael Kidd-Gilchrist', 'Lance Stephenson', 'Joe Johnson', 'Blake Griffin', 'Darrell Arthur', 'Chris Paul', 'D.J. Augustin']
['Joakim Noah', 'Wilson Chandler', 'Michael Kidd-Gilchrist', 'Lance Stephenson', 'Kyle Korver', 'Blake Griffin', 'Terrence Jones', 'Chris Paul', 'Shaun Livingston']
['Joakim Noah', 'Wilson Chandler', 'Michael Kidd-Gilchrist', 'Lance Stephenson', 'Kyle Korver', 'Blake Griffin', 'Kris Humphries', 'Chris Paul', 'Jerryd Bayless']
['Joakim Noah', 'Wilson Chandler', 'Michael Kidd-Gilchrist', 'Gordon Hayward', 'Joe Johnson', 'Blake Griffin', 'Andray Blatche', 'Chris Paul', 'Jerryd Bayless']
['Joakim Noah', 'Wilson Chandler', 'Michael Kidd-Gilchrist', 'Gordon Hayward', 'Kyle Korver', 'Blake Griffin', 'Andray Blatche', 'Chris Paul', 'Avery Bradley']

这些是我导出的列表。我想将它们作为列表列表重新导入另一个脚本。所以我基本上想要制作一个包含这些列表的列表。这可能吗?当我现在尝试并阅读它时,它会将其读作字符串。

2 个答案:

答案 0 :(得分:3)

您可以使用ast.literal_eval()

  

安全地评估表达式节点或Unicode或Latin-1编码   包含Python表达式的字符串。提供的字符串或节点可以   只包含以下Python文字结构:字符串,   数字,元组,列表,dicts,布尔值和无。

from ast import literal_eval

with open('test.txt', 'rb') as f:
    data = [literal_eval(line) for line in f]

print data

产生

[
 ['Joakim Noah', 'Wilson Chandler', 'Michael Kidd-Gilchrist', 'Lance Stephenson', 'Joe Johnson', 'Blake Griffin', 'Darrell Arthur', 'Chris Paul', 'D.J. Augustin'], 
 ['Joakim Noah', 'Wilson Chandler', 'Michael Kidd-Gilchrist', 'Lance Stephenson', 'Kyle Korver', 'Blake Griffin', 'Terrence Jones', 'Chris Paul', 'Shaun Livingston'], 
 ['Joakim Noah', 'Wilson Chandler', 'Michael Kidd-Gilchrist', 'Lance Stephenson', 'Kyle Korver', 'Blake Griffin', 'Kris Humphries', 'Chris Paul', 'Jerryd Bayless'], 
 ['Joakim Noah', 'Wilson Chandler', 'Michael Kidd-Gilchrist', 'Gordon Hayward', 'Joe Johnson', 'Blake Griffin', 'Andray Blatche', 'Chris Paul', 'Jerryd Bayless'], 
 ['Joakim Noah', 'Wilson Chandler', 'Michael Kidd-Gilchrist', 'Gordon Hayward', 'Kyle Korver', 'Blake Griffin', 'Andray Blatche', 'Chris Paul', 'Avery Bradley']
]

另外,请考虑使用picklejson序列化/反序列化。

答案 1 :(得分:0)

你也可以这样做:

    from os.path import dirname

    with open(dirname(__file__) + '/data/path/demo.txt') as input_data:
        A= [line.strip() for line in input_data.readlines()]

产地:

[" [' Joakim Noah',' Wilson Chandler',' Michael Kidd-Gilchrist',' Lance Stephenson' ,' Joe Johnson' Blake Griffin' Darrell Arthur' Chris Paul',' DJ Augustin']"," [' Joakim Noah',' Wilson Chandler',' Michael Kidd-Gilchrist',' Lance Stephenson',' Kyle Korver' Blake Griffin' Terrence Jones' Chris Paul',' ; Shaun Livingston"]"," [' Joakim Noah',' Wilson Chandler',' Michael Kidd-Gilchrist',& #39; Lance Stephenson' Kyle Korver',' Blake Griffin' Kris Humphries',' Chris Paul',& #39; Jerryd Bayless']"," [' Joakim Noah',' Wilson Chandler',' Michael Kidd-Gilchrist' ,' Gordon Hayward' Joe Johnson',' Blake Griffin' Andray Blatche',' Chris Paul' ,' Jerryd Bayless']"," [' Joakim Noah',' Wilson Chandler',' Michael Kidd-Gilchrist&# 39;,' Gordon Hayward' Kyle Korver',' Blake Griffin' Andray Blatche',' Chris Paul' 39;,' Avery Bradley']" ]