从文件中获取列表?

时间:2017-06-01 14:54:24

标签: python file-io

在我制作的刽子手游戏中,我有一个包含以下内容的文本文件:

[
[
['microsoft windows','common operating system',1],
['apple ios,a useless operating system',1],
['chromeos','a very simple OS developed by chrome',1],
["linux","the most accesable and custimizable OS",1]
],
[
["photoshop", "the most popular image editting program",2],
["adobe flash","basic animating and programming program",2],
["pineapple","a spikey sweet fruit",2],
["chrome", "a web browser",2]
],
[
["gpu","a computers _____ proccesing unit", 3],
["tree", "a large plant found every where",3],
["kangaroo","a marsupial found in the outback",3],
["hawiian pizza","a very disputed type of triangular food",3]
],
[
["negev","a light machine gun, from csgo", 4],
["revolver","a high caliber hand canon", 4],
["desert eagle", "a infamous hand canon, highly praised in the USA", 4],
["karambit knife","a blade shaped like a claw, know as very deadly", 4]
]
]

所以使用文件io,我写了这个:

f = open('words.py')
lines = f.readlines()
for line in lines:
   cat1.append(line)
print(cat1)

但我得到的只是:

['[\n', '[\n', "['microsoft windows','common operating system',1],\n", "['apple ios,a useless operating system',1],\n", "['chromeos','a very simple OS developed by chrome',1],\n", '["linux","the most accesable and custimizable OS",1]\n', '],\n', '[\n', '["photoshop", "the most popular image editting program",2],\n', '["adobe flash","basic animating and programming program",2],\n', '["pineapple","a spikey sweet fruit",2],\n', '["chrome", "a web browser",2]\n', '],\n', '[\n', '["gpu","a computers _____ proccesing unit", 3],\n', '["tree", "a large plant found every where",3],\n', '["kangaroo","a marsupial found in the outback",3],\n', '["hawiian pizza","a very disputed type of triangular food",3]\n', '],\n', '[\n', '["negev","a light machine gun, from csgo", 4],\n', '["revolver","a high caliber hand canon", 4],\n', '["desert eagle", "a infamous hand canon, highly praised in the USA", 4],\n', '["karambit knife","a blade shaped like a claw, know as very deadly", 4]\n', ']\n', ']\n']

如何将其分为4个子组列表,这些子组也在4个子列表中,最后我如何删除'\n'

2 个答案:

答案 0 :(得分:0)

如果您将第3,第4和第5行的单引号更改为双引号,那么您的文件将是有效的json,在这种情况下,您可以使用json模块轻松解析整个事件:

data.json:

        [
        [
        ["microsoft windows","common operating system",1],
        ["apple ios,a useless operating system",1],
        ["chromeos","a very simple OS developed by chrome",1],
        ["linux","the most accesable and custimizable OS",1]
        ],
        [
        ["photoshop", "the most popular image editting program",2],
        ["adobe flash","basic animating and programming program",2],
        ["pineapple","a spikey sweet fruit",2],
        ["chrome", "a web browser",2]
        ],
        [
        ["gpu","a computers _____ proccesing unit", 3],
        ["tree", "a large plant found every where",3],
        ["kangaroo","a marsupial found in the outback",3],
        ["hawiian pizza","a very disputed type of triangular food",3]
        ],
        [
        ["negev","a light machine gun, from csgo", 4],
        ["revolver","a high caliber hand canon", 4],
        ["desert eagle", "a infamous hand canon, highly praised in the USA", 4],
        ["karambit knife","a blade shaped like a claw, know as very deadly", 4]
        ]
        ]

你的剧本:

import json
with open("data.json") as file:
    seq = json.load(file)
print(seq)

结果:

[[['microsoft windows', 'common operating system', 1], ['apple ios,a useless operating system', 1], ['chromeos', 'a very simple OS developed by chrome', 1], ['linux', 'the most accesable and custimizable OS', 1]], [['photoshop', 'the most popular image editting program', 2], ['adobe flash', 'basic animating and programming program', 2], ['pineapple', 'a spikey sweet fruit', 2], ['chrome', 'a web browser', 2]], [['gpu', 'a computers _____ proccesing unit', 3], ['tree', 'a large plant found every where', 3], ['kangaroo', 'a marsupial found in the outback', 3], ['hawiian pizza', 'a very disputed type of triangular food', 3]], [['negev', 'a light machine gun, from csgo', 4], ['revolver', 'a high caliber hand canon', 4], ['desert eagle', 'a infamous hand canon, highly praised in the USA', 4], ['karambit knife', 'a blade shaped like a claw, know as very deadly', 4]]]

如果您绝对承诺不更改文件中的任何内容,包括引号,那么我认为您也可以使用ast.literal_eval。但这不是惯用的解决方案。

import ast
with open("data.dat") as file:
    seq = ast.literal_eval(file.read())
print(seq)

顺便说一下,['apple ios,a useless operating system',1]是有效的语法,但是如果你希望这个元素的长度为3,就像它的兄弟一样,你可能想写['apple ios','a useless operating system',1]

答案 1 :(得分:0)

该文件包含有效的Python文字,因此您可以使用ast..literal_eval()函数将其直接解析为list

import ast

with open('words.py') as file:
    cat1 = ast.literal_eval(file.read())

print(cat1)

您还可以将文件的第一行更改为cat1 = [,然后使用

from words import cat1

声明而不是完成所有这些。