从文件中获取列表?

2024-04-26 21:19:40 发布

您现在位置:Python中文网/ 问答频道 /正文

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

[
[
['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'


Tags: andtheinmostoswindowsoperatingchrome
2条回答

该文件包含有效的Python文本,因此可以使用^{}函数将其直接解析为list

import ast

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

print(cat1)

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

from words import cat1

声明而不是这么做

如果将第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]是有效的语法,但是如果您想让这个元素像它的同级元素一样有三个长度,那么您可能打算编写['apple ios','a useless operating system',1]

相关问题 更多 >