首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从文件中获取列表?

从文件中获取列表?
EN

Stack Overflow用户
提问于 2017-06-01 22:54:24
回答 2查看 34关注 0票数 0

在我正在制作的一个绞刑者游戏中,我有一个文本文件,其中包含以下内容:

代码语言:javascript
复制
[
[
['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,我编写了以下代码:

代码语言:javascript
复制
f = open('words.py')
lines = f.readlines()
for line in lines:
   cat1.append(line)
print(cat1)

但我得到的只是:

代码语言:javascript
复制
['[\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'

EN

回答 2

Stack Overflow用户

发布于 2017-06-01 22:57:32

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

data.json:

代码语言:javascript
复制
        [
        [
        ["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]
        ]
        ]

你的脚本:

代码语言:javascript
复制
import json
with open("data.json") as file:
    seq = json.load(file)
print(seq)

结果:

代码语言:javascript
复制
[[['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。但这不是惯用的解决方案。

代码语言:javascript
复制
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]

票数 0
EN

Stack Overflow用户

发布于 2017-06-01 23:09:41

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

代码语言:javascript
复制
import ast

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

print(cat1)

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

代码语言:javascript
复制
from words import cat1

语句,而不是执行所有这些操作。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44310884

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档